mt3842ml commited on
Commit
1606bed
·
verified ·
1 Parent(s): 7e2a637

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -178,18 +178,30 @@ def get_docs(query: str, top_k: int) -> list[str]:
178
  from groq import Groq
179
  groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
180
 
181
- def generate(query: str, docs: list[str]):
 
 
 
 
 
 
 
 
 
182
  system_message = (
183
  "Pretend you are a friend that lives in New York City. "
184
- "Please answer while prioritizing the "
185
  "context provided below.\n\n"
186
  "CONTEXT:\n"
187
- "\n---\n".join(docs)
188
  )
189
  messages = [
190
  {"role": "system", "content": system_message},
191
- {"role": "user", "content": query}
192
  ]
 
 
 
 
193
  # generate response
194
  chat_response = groq_client.chat.completions.create(
195
  model="llama3-70b-8192",
@@ -197,33 +209,6 @@ def generate(query: str, docs: list[str]):
197
  )
198
  return chat_response.choices[0].message.content
199
 
200
- query = "Favorite drink in Queens?"
201
- docs = get_docs(query, top_k=5)
202
- out = generate(query=query, docs=docs)
203
-
204
-
205
- #### to implement
206
-
207
- def chat_with_groq(user_input, history):
208
-
209
- client = Groq(api_key=api_key)
210
-
211
- # Format the history into Groq's expected format
212
- messages = [{"role": "system", "content": "You are a helpful assistant."}]
213
- for user_msg, bot_msg in history:
214
- messages.append({"role": "user", "content": user_msg})
215
- messages.append({"role": "assistant", "content": bot_msg})
216
-
217
- # Append the latest user input
218
- messages.append({"role": "user", "content": user_input})
219
-
220
- chat_completion = client.chat.completions.create(
221
- messages=messages,
222
- model="llama-3.3-70b-versatile",
223
- )
224
-
225
- response = chat_completion.choices[0].message.content
226
- return response
227
 
228
  # Custom CSS for iPhone-style chat
229
  custom_css = """
@@ -267,6 +252,6 @@ custom_css = """
267
  """
268
 
269
  # Gradio Interface
270
- demo = gr.ChatInterface(chat_with_groq, css=custom_css)
271
 
272
  demo.launch()
 
178
  from groq import Groq
179
  groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
180
 
181
+ def generate(query: str, history):
182
+
183
+ if !history:
184
+ return "History doesn't exist"
185
+
186
+ # Establish history
187
+ for user_msg, bot_msg in history:
188
+ messages.append({"role": "user", "content": user_msg})
189
+ messages.append({"role": "assistant", "content": bot_msg})
190
+
191
  system_message = (
192
  "Pretend you are a friend that lives in New York City. "
193
+ "Please answer while prioritizing fun and unique answers using the "
194
  "context provided below.\n\n"
195
  "CONTEXT:\n"
196
+ "\n---\n".join(get_docs(query, top_k=5))
197
  )
198
  messages = [
199
  {"role": "system", "content": system_message},
 
200
  ]
201
+
202
+ # Add query
203
+ messages.append({"role": "user", "content": query})
204
+
205
  # generate response
206
  chat_response = groq_client.chat.completions.create(
207
  model="llama3-70b-8192",
 
209
  )
210
  return chat_response.choices[0].message.content
211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
 
213
  # Custom CSS for iPhone-style chat
214
  custom_css = """
 
252
  """
253
 
254
  # Gradio Interface
255
+ demo = gr.ChatInterface(generate, css=custom_css)
256
 
257
  demo.launch()