Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,7 @@ client = Client("on1onmangoes/CNIHUB101324v10", hf_token=HF_TOKEN)
|
|
16 |
# Update the conversation history within the function.
|
17 |
# Return the updated history along with any other required outputs.
|
18 |
|
|
|
19 |
def stream_chat_with_rag(
|
20 |
message: str,
|
21 |
history: list,
|
@@ -23,123 +24,51 @@ def stream_chat_with_rag(
|
|
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" +
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
answer = response[0]
|
67 |
-
else:
|
68 |
-
answer = response
|
69 |
-
|
70 |
-
# Debugging statements
|
71 |
-
print("The Answer in stream_chat_with_rag:")
|
72 |
-
print(answer)
|
73 |
-
|
74 |
-
# Update the conversation history
|
75 |
-
history.append((message, answer))
|
76 |
-
|
77 |
-
except Exception as e:
|
78 |
-
print(f"An error occurred: {e}")
|
79 |
-
answer = "There was an error retrieving the response."
|
80 |
-
|
81 |
-
# # Return the updated history
|
82 |
-
# return history
|
83 |
-
|
84 |
-
|
85 |
-
# def stream_chat_with_rag(
|
86 |
-
# message: str,
|
87 |
-
# history: list,
|
88 |
-
# client_name: str,
|
89 |
-
# system_prompt: str,
|
90 |
-
# num_retrieved_docs: int = 10,
|
91 |
-
# num_docs_final: int = 9,
|
92 |
-
# temperature: float = 0,
|
93 |
-
# max_new_tokens: int = 1024,
|
94 |
-
# top_p: float = 1.0,
|
95 |
-
# top_k: int = 20,
|
96 |
-
# penalty: float = 1.2,
|
97 |
-
# ):
|
98 |
-
# print(f"Message: {message}")
|
99 |
-
# print(f"History: {history}")
|
100 |
-
|
101 |
-
# # Build the conversation prompt including system prompt and history
|
102 |
-
# conversation = system_prompt + "\n\n" + f"For Client: {client_name}\n"
|
103 |
-
# for user_input, assistant_response in history:
|
104 |
-
# conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
|
105 |
-
# conversation += f"User: {message}\nAssistant:"
|
106 |
-
|
107 |
-
# # Prepare the data to send to the API
|
108 |
-
# # Remove 'history' from the payload since the API does not accept it
|
109 |
-
# api_payload = {
|
110 |
-
# "message": conversation, # Include the history in the message
|
111 |
-
# "client_name": client_name,
|
112 |
-
# "system_prompt": "", # Optionally set to empty if included in message
|
113 |
-
# "num_retrieved_docs": num_retrieved_docs,
|
114 |
-
# "num_docs_final": num_docs_final,
|
115 |
-
# "temperature": temperature,
|
116 |
-
# "max_new_tokens": max_new_tokens,
|
117 |
-
# "top_p": top_p,
|
118 |
-
# "top_k": top_k,
|
119 |
-
# "penalty": penalty,
|
120 |
-
# }
|
121 |
-
|
122 |
-
# # Make the API call to get the assistant's reply
|
123 |
-
# response = client.predict(
|
124 |
-
# api_name="/chat",
|
125 |
-
# **api_payload
|
126 |
-
# )
|
127 |
-
|
128 |
-
# # Extract the assistant's reply
|
129 |
-
# if isinstance(response, tuple):
|
130 |
-
# answer = response[0]
|
131 |
-
# else:
|
132 |
-
# answer = response
|
133 |
-
|
134 |
-
# # Debugging statements
|
135 |
-
# print("The Answer in stream_chat_with_rag:")
|
136 |
-
# print(answer)
|
137 |
-
|
138 |
-
# # Update the conversation history
|
139 |
-
# history.append((message, answer))
|
140 |
-
|
141 |
-
# # Return the updated history
|
142 |
-
# #return history
|
143 |
|
144 |
|
145 |
# Function to handle PDF processing API call
|
|
|
16 |
# Update the conversation history within the function.
|
17 |
# Return the updated history along with any other required outputs.
|
18 |
|
19 |
+
@spaces.GPU()
|
20 |
def stream_chat_with_rag(
|
21 |
message: str,
|
22 |
history: list,
|
|
|
24 |
system_prompt: str,
|
25 |
num_retrieved_docs: int = 10,
|
26 |
num_docs_final: int = 9,
|
27 |
+
temperature: float = 0,
|
28 |
+
max_new_tokens: int = 1024,
|
29 |
+
top_p: float = 1.0,
|
30 |
+
top_k: int = 20,
|
31 |
penalty: float = 1.2,
|
32 |
):
|
33 |
print(f"Message: {message}")
|
34 |
print(f"History: {history}")
|
35 |
|
36 |
+
# OG CODE DELETE
|
37 |
+
# # Add the knowledge Index or VectorStore, RERANKER,
|
38 |
+
# knowledge_index = vectorstore
|
39 |
+
# reranker = RERANKER
|
40 |
+
|
41 |
# Build the conversation prompt including system prompt and history
|
42 |
+
conversation = system_prompt + "\n\n" + "For Client:" + client_name
|
43 |
+
for user_input, assistant_response in history:
|
44 |
+
conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
|
45 |
+
conversation += f"User: {message}\nAssistant:"
|
46 |
+
|
47 |
+
# Optionally, if your `answer_with_rag` function or LLM supports context, you can include the conversation
|
48 |
+
# Since you prefer not to modify `answer_with_rag`, we'll proceed with the message as is
|
49 |
+
# OG CODE DELETE
|
50 |
+
# # Call `answer_with_rag` to get the final answer
|
51 |
+
# answer, relevant_docs = answer_with_rag(
|
52 |
+
# question=message,
|
53 |
+
# knowledge_index=knowledge_index,
|
54 |
+
# reranker=reranker,
|
55 |
+
# num_retrieved_docs=num_retrieved_docs,
|
56 |
+
# num_docs_final=num_docs_final,
|
57 |
+
# client_name=client_name,
|
58 |
+
# )
|
59 |
+
|
60 |
+
|
61 |
+
answer, relevant_docs = client.predict(question=question, api_name="/answer_with_rag")
|
62 |
+
# debug 092624
|
63 |
+
print("The Answer in stream_chat_with_rag:")
|
64 |
+
print(answer)
|
65 |
+
|
66 |
+
print("The relevant_doc:")
|
67 |
+
print(relevant_docs)
|
68 |
+
|
69 |
+
# Update the conversation history
|
70 |
+
history.append((message, answer))
|
71 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
|
74 |
# Function to handle PDF processing API call
|