essbony commited on
Commit
333acbb
·
verified ·
1 Parent(s): f2f9ede

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -71,7 +71,8 @@ retriever=faiss_db.as_retriever(search_type="similarity", search_kwargs={"k":3})
71
  llm = ChatGroq(
72
  model="llama-3.3-70b-versatile",
73
  temperature=0.0,
74
- max_tokens=1200
 
75
  )
76
 
77
  prompt = ChatPromptTemplate.from_messages([
@@ -120,14 +121,31 @@ convers_chain = RunnableWithMessageHistory(
120
 
121
  SESSION_ID = str(uuid.uuid4()) # session globale
122
 
 
 
 
 
 
 
 
123
  def chat_fn(message, history):
124
- result = convers_chain.invoke(
 
 
 
125
  {"input": message},
126
  config={"configurable": {"session_id": SESSION_ID}}
127
- )
128
- return result.get("answer", str(result))
129
-
130
-
 
 
 
 
 
 
 
131
 
132
 
133
  # ================= GRADIO ====================
 
71
  llm = ChatGroq(
72
  model="llama-3.3-70b-versatile",
73
  temperature=0.0,
74
+ max_tokens=1200,
75
+ streaming=True
76
  )
77
 
78
  prompt = ChatPromptTemplate.from_messages([
 
121
 
122
  SESSION_ID = str(uuid.uuid4()) # session globale
123
 
124
+ # def chat_fn(message, history):
125
+ # result = convers_chain.invoke(
126
+ # {"input": message},
127
+ # config={"configurable": {"session_id": SESSION_ID}}
128
+ # )
129
+ # return result.get("answer", str(result))
130
+
131
  def chat_fn(message, history):
132
+ """Chat avec streaming optimisé"""
133
+ response = ""
134
+
135
+ for chunk in convers_chain.stream(
136
  {"input": message},
137
  config={"configurable": {"session_id": SESSION_ID}}
138
+ ):
139
+ # Gestion des différents types de chunks possibles
140
+ if isinstance(chunk, dict):
141
+ token = chunk.get("answer") or chunk.get("content") or str(chunk)
142
+ elif hasattr(chunk, "content"): # Pour AIMessage
143
+ token = chunk.content
144
+ else:
145
+ token = str(chunk)
146
+
147
+ response += token
148
+ yield response
149
 
150
 
151
  # ================= GRADIO ====================