on1onmangoes commited on
Commit
de6817d
·
verified ·
1 Parent(s): 71991de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -111
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" + f"For Client: {client_name}\n"
37
- if history: # Check if history exists
38
- for user_input, assistant_response in history:
39
- conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
40
- conversation += f"User: {message}\nAssistant:" # Add the current message
41
-
42
- # Prepare the data to send to the API
43
- api_payload = {
44
- "message": conversation, # Include the history in the message,
45
- "history": history,
46
- "client_name": client_name,
47
- "system_prompt": "", # Optionally set to empty if included in the message
48
- "num_retrieved_docs": num_retrieved_docs,
49
- "num_docs_final": num_docs_final,
50
- "temperature": temperature,
51
- "max_new_tokens": max_new_tokens,
52
- "top_p": top_p,
53
- "top_k": top_k,
54
- "penalty": penalty,
55
- }
56
-
57
- try:
58
- # Make the API call to get the assistant's reply
59
- response = client.predict(
60
- api_name="/chat",
61
- **api_payload
62
- )
63
-
64
- # Extract the assistant's reply
65
- if isinstance(response, tuple):
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