on1onmangoes commited on
Commit
6464518
·
verified ·
1 Parent(s): d18ec92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -98
app.py CHANGED
@@ -21,115 +21,61 @@ def stream_chat_with_rag(
21
  history: list,
22
  client_name: str,
23
  system_prompt: str,
24
- num_retrieved_docs: int,
25
- num_docs_final: int,
26
- temperature: float,
27
- max_new_tokens: int,
28
- top_p: float,
29
- top_k: int,
30
- penalty: float,
31
  ):
32
- # Use the parameters provided by the UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  response = client.predict(
34
- message=message,
35
- client_name=client_name,
36
- system_prompt=system_prompt,
37
- num_retrieved_docs=num_retrieved_docs,
38
- num_docs_final=num_docs_final,
39
- temperature=temperature,
40
- max_new_tokens=max_new_tokens,
41
- top_p=top_p,
42
- top_k=top_k,
43
- penalty=penalty,
44
- api_name="/chat"
45
  )
46
 
47
- # Debugging statements
48
- print(f"API Response Type: {type(response)}")
49
- print(f"API Response: {response}")
 
 
 
50
 
51
- # Ensure response is a string
52
- if isinstance(response, list):
53
- # If response is a list, extract the assistant's reply
54
- response = response[0] if response else ""
55
 
56
  # Update the conversation history
57
- history = history + [(message, response)]
58
 
59
  # Return the updated history
60
  return history
61
 
62
-
63
-
64
-
65
- # # Function to handle chat API call
66
- # # Function to handle chat API call
67
- # def stream_chat_with_rag(
68
- # message: str,
69
- # history: list,
70
- # client_name: str,
71
- # system_prompt: str,
72
- # num_retrieved_docs: int,
73
- # num_docs_final: int,
74
- # temperature: float,
75
- # max_new_tokens: int,
76
- # top_p: float,
77
- # top_k: int,
78
- # penalty: float,
79
- # ):
80
- # # Use the parameters provided by the UI
81
- # response = client.predict(
82
- # message=message,
83
- # client_name=client_name,
84
- # system_prompt=system_prompt,
85
- # num_retrieved_docs=num_retrieved_docs,
86
- # num_docs_final=num_docs_final,
87
- # temperature=temperature,
88
- # max_new_tokens=max_new_tokens,
89
- # top_p=top_p,
90
- # top_k=top_k,
91
- # penalty=penalty,
92
- # api_name="/chat"
93
- # )
94
-
95
- # # Update the conversation history
96
- # history = history + [(message, response)]
97
-
98
- # # Return the assistant's reply and the updated history
99
- # return "", history
100
-
101
-
102
- # # Function to handle chat API call
103
- # def stream_chat_with_rag(
104
- # message: str,
105
- # history: list,
106
- # client_name: str,
107
- # system_prompt: str,
108
- # num_retrieved_docs: int,
109
- # num_docs_final: int,
110
- # temperature: float,
111
- # max_new_tokens: int,
112
- # top_p: float,
113
- # top_k: int,
114
- # penalty: float,
115
- # ):
116
- # # Use the parameters provided by the UI
117
- # response = client.predict(
118
- # message=message,
119
- # client_name=client_name,
120
- # system_prompt=system_prompt,
121
- # num_retrieved_docs=num_retrieved_docs,
122
- # num_docs_final=num_docs_final,
123
- # temperature=temperature,
124
- # max_new_tokens=max_new_tokens,
125
- # top_p=top_p,
126
- # top_k=top_k,
127
- # penalty=penalty,
128
- # api_name="/chat"
129
- # )
130
- # # Return the assistant's reply
131
- # return response
132
-
133
  # Function to handle PDF processing API call
134
  def process_pdf(pdf_file):
135
  return client.predict(
 
21
  history: list,
22
  client_name: str,
23
  system_prompt: str,
24
+ num_retrieved_docs: int = 10,
25
+ num_docs_final: int = 9,
26
+ temperature: float = 0,
27
+ max_new_tokens: int = 1024,
28
+ top_p: float = 1.0,
29
+ top_k: int = 20,
30
+ penalty: float = 1.2,
31
  ):
32
+ print(f"Message: {message}")
33
+ print(f"History: {history}")
34
+
35
+ # Build the conversation prompt including system prompt and history
36
+ conversation = system_prompt + "\n\n" + "For Client: " + client_name + "\n"
37
+ for user_input, assistant_response in history:
38
+ conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
39
+ conversation += f"User: {message}\nAssistant:"
40
+
41
+ # Prepare the data to send to the API
42
+ api_payload = {
43
+ "message": message,
44
+ "history": history,
45
+ "client_name": client_name,
46
+ "system_prompt": system_prompt,
47
+ "num_retrieved_docs": num_retrieved_docs,
48
+ "num_docs_final": num_docs_final,
49
+ "temperature": temperature,
50
+ "max_new_tokens": max_new_tokens,
51
+ "top_p": top_p,
52
+ "top_k": top_k,
53
+ "penalty": penalty,
54
+ }
55
+
56
+ # Make the API call to get the assistant's reply
57
  response = client.predict(
58
+ api_name="/chat",
59
+ **api_payload
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
+ # Assuming the API returns the assistant's reply
63
+ # If the API returns a tuple (answer, relevant_docs), extract the answer
64
+ if isinstance(response, tuple):
65
+ answer = response[0]
66
+ else:
67
+ answer = response # If it's already the assistant's reply
68
 
69
+ # Debugging statements
70
+ print("The Answer in stream_chat_with_rag:")
71
+ print(answer)
 
72
 
73
  # Update the conversation history
74
+ history.append((message, answer))
75
 
76
  # Return the updated history
77
  return history
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  # Function to handle PDF processing API call
80
  def process_pdf(pdf_file):
81
  return client.predict(