Spaces:
Sleeping
Sleeping
Ganesh Chintalapati
commited on
Commit
·
1d36e10
1
Parent(s):
9fc2d35
gpt-4o-mini streaming
Browse files
app.py
CHANGED
@@ -24,7 +24,6 @@ async def ask_openai(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
24 |
yield "Error: OpenAI API key not provided."
|
25 |
return
|
26 |
|
27 |
-
# Build message history
|
28 |
messages = []
|
29 |
for msg in history:
|
30 |
messages.append({"role": "user", "content": msg["user"]})
|
@@ -49,7 +48,6 @@ async def ask_openai(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
49 |
response.raise_for_status()
|
50 |
async for chunk in response.aiter_text():
|
51 |
if chunk:
|
52 |
-
# Parse the streaming chunk (JSON lines)
|
53 |
lines = chunk.splitlines()
|
54 |
for line in lines:
|
55 |
if line.startswith("data: "):
|
@@ -59,23 +57,19 @@ async def ask_openai(query: str, history: List[Dict[str, str]]) -> AsyncGenerato
|
|
59 |
if not data.strip():
|
60 |
continue
|
61 |
try:
|
62 |
-
json_data = json.loads(data)
|
63 |
if "choices" in json_data and json_data["choices"]:
|
64 |
delta = json_data["choices"][0].get("delta", {})
|
65 |
if "content" in delta and delta["content"] is not None:
|
66 |
yield delta["content"]
|
67 |
except json.JSONDecodeError as e:
|
68 |
-
logger.error(f"Error parsing OpenAI stream chunk: {str(e)} - Data: {data}")
|
69 |
yield f"Error parsing stream: {str(e)}"
|
70 |
except Exception as e:
|
71 |
-
logger.error(f"Unexpected error in OpenAI stream: {str(e)} - Data: {data}")
|
72 |
yield f"Error in stream: {str(e)}"
|
73 |
|
74 |
except httpx.HTTPStatusError as e:
|
75 |
-
|
76 |
-
yield f"Error: OpenAI HTTP Status Error: {e.response.status_code}, {e.response.text}"
|
77 |
except Exception as e:
|
78 |
-
logger.error(f"OpenAI Error: {str(e)}")
|
79 |
yield f"Error: OpenAI Error: {str(e)}"
|
80 |
|
81 |
async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> str:
|
@@ -84,7 +78,6 @@ async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> str:
|
|
84 |
logger.error("Anthropic API key not provided")
|
85 |
return "Error: Anthropic API key not provided."
|
86 |
|
87 |
-
# Build message history
|
88 |
messages = []
|
89 |
for msg in history:
|
90 |
messages.append({"role": "user", "content": msg["user"]})
|
@@ -126,7 +119,6 @@ async def ask_gemini(query: str, history: List[Dict[str, str]]) -> str:
|
|
126 |
logger.error("Gemini API key not provided")
|
127 |
return "Error: Gemini API key not provided."
|
128 |
|
129 |
-
# Gemini doesn't natively support chat history in the same way, so we concatenate history as text
|
130 |
history_text = ""
|
131 |
for msg in history:
|
132 |
history_text += f"User: {msg['user']}\nAssistant: {msg['bot']}\n" if msg["bot"] else f"User: {msg['user']}\n"
|
@@ -160,11 +152,9 @@ async def ask_gemini(query: str, history: List[Dict[str, str]]) -> str:
|
|
160 |
|
161 |
async def query_model(query: str, provider: str, history: List[Dict[str, str]]) -> AsyncGenerator[Tuple[str, List[Dict[str, str]]], None]:
|
162 |
provider = provider.lower()
|
163 |
-
response = ""
|
164 |
|
165 |
if provider == "openai":
|
166 |
async for chunk in ask_openai(query, history):
|
167 |
-
response += chunk
|
168 |
yield chunk, history # Yield partial response for streaming
|
169 |
elif provider == "anthropic":
|
170 |
response = await ask_anthropic(query, history)
|
@@ -187,25 +177,22 @@ async def submit_query(query: str, provider: str, history: List[Dict[str, str]])
|
|
187 |
return
|
188 |
|
189 |
response = ""
|
|
|
190 |
async for response_chunk, updated_history in query_model(query, provider, history):
|
191 |
response += response_chunk
|
|
|
192 |
# Convert history to chatbot messages format
|
193 |
chatbot_messages = []
|
194 |
for msg in updated_history:
|
195 |
chatbot_messages.append({"role": "user", "content": msg["user"]})
|
196 |
if msg["bot"]:
|
197 |
chatbot_messages.append({"role": "assistant", "content": msg["bot"]})
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
chatbot_messages
|
204 |
-
for msg in updated_history:
|
205 |
-
chatbot_messages.append({"role": "user", "content": msg["user"]})
|
206 |
-
if msg["bot"]:
|
207 |
-
chatbot_messages.append({"role": "assistant", "content": msg["bot"]})
|
208 |
-
yield "", chatbot_messages, updated_history
|
209 |
|
210 |
# Gradio interface
|
211 |
def clear_history():
|
|
|
24 |
yield "Error: OpenAI API key not provided."
|
25 |
return
|
26 |
|
|
|
27 |
messages = []
|
28 |
for msg in history:
|
29 |
messages.append({"role": "user", "content": msg["user"]})
|
|
|
48 |
response.raise_for_status()
|
49 |
async for chunk in response.aiter_text():
|
50 |
if chunk:
|
|
|
51 |
lines = chunk.splitlines()
|
52 |
for line in lines:
|
53 |
if line.startswith("data: "):
|
|
|
57 |
if not data.strip():
|
58 |
continue
|
59 |
try:
|
60 |
+
json_data = json.loads(data)
|
61 |
if "choices" in json_data and json_data["choices"]:
|
62 |
delta = json_data["choices"][0].get("delta", {})
|
63 |
if "content" in delta and delta["content"] is not None:
|
64 |
yield delta["content"]
|
65 |
except json.JSONDecodeError as e:
|
|
|
66 |
yield f"Error parsing stream: {str(e)}"
|
67 |
except Exception as e:
|
|
|
68 |
yield f"Error in stream: {str(e)}"
|
69 |
|
70 |
except httpx.HTTPStatusError as e:
|
71 |
+
yield f"Error: OpenAI HTTP Status Error: {e.response.status_code}"
|
|
|
72 |
except Exception as e:
|
|
|
73 |
yield f"Error: OpenAI Error: {str(e)}"
|
74 |
|
75 |
async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> str:
|
|
|
78 |
logger.error("Anthropic API key not provided")
|
79 |
return "Error: Anthropic API key not provided."
|
80 |
|
|
|
81 |
messages = []
|
82 |
for msg in history:
|
83 |
messages.append({"role": "user", "content": msg["user"]})
|
|
|
119 |
logger.error("Gemini API key not provided")
|
120 |
return "Error: Gemini API key not provided."
|
121 |
|
|
|
122 |
history_text = ""
|
123 |
for msg in history:
|
124 |
history_text += f"User: {msg['user']}\nAssistant: {msg['bot']}\n" if msg["bot"] else f"User: {msg['user']}\n"
|
|
|
152 |
|
153 |
async def query_model(query: str, provider: str, history: List[Dict[str, str]]) -> AsyncGenerator[Tuple[str, List[Dict[str, str]]], None]:
|
154 |
provider = provider.lower()
|
|
|
155 |
|
156 |
if provider == "openai":
|
157 |
async for chunk in ask_openai(query, history):
|
|
|
158 |
yield chunk, history # Yield partial response for streaming
|
159 |
elif provider == "anthropic":
|
160 |
response = await ask_anthropic(query, history)
|
|
|
177 |
return
|
178 |
|
179 |
response = ""
|
180 |
+
|
181 |
async for response_chunk, updated_history in query_model(query, provider, history):
|
182 |
response += response_chunk
|
183 |
+
|
184 |
# Convert history to chatbot messages format
|
185 |
chatbot_messages = []
|
186 |
for msg in updated_history:
|
187 |
chatbot_messages.append({"role": "user", "content": msg["user"]})
|
188 |
if msg["bot"]:
|
189 |
chatbot_messages.append({"role": "assistant", "content": msg["bot"]})
|
190 |
+
|
191 |
+
# Yield incremental updates for streaming
|
192 |
+
yield response_chunk, chatbot_messages, updated_history
|
193 |
+
|
194 |
+
updated_history.append({"user": query, "bot": response})
|
195 |
+
yield response, chatbot_messages, updated_history
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
# Gradio interface
|
198 |
def clear_history():
|