Spaces:
Runtime error
Runtime error
Ganesh Chintalapati commited on
Commit ·
da5b2ad
1
Parent(s): 6e59463
adding streaming
Browse files48 unmodified lines495051525354553 unmodified lines5960616263646512 unmodified lines7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623748 unmodified linesresponse.raise_for_status()async for chunk in response.aiter_text():if chunk:# Parse the streaming chunk (JSON lines)lines = chunk.splitlines()for line in lines:if line.startswith("data: "):3 unmodified linesif not data.strip():continuetry:json_data = json.loads(data) # Safely parse JSONif "choices" in json_data and json_data["choices"]:delta = json_data["choices"][0].get("delta", {})if "content" in delta and delta["content"] is not None:12 unmodified lineslogger.error(f"OpenAI Error: {str(e)}")yield f"Error: OpenAI Error: {str(e)}"async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> str:anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")if not anthropic_api_key:logger.error("Anthropic API key not provided")return "Error: Anthropic API key not provided."# Build message historymessages = []for msg in history:messages.append({"role": "user", "content": msg["user"]})if msg["bot"]:messages.append({"role": "assistant", "content": msg["bot"]})messages.append({"role": "user", "content": query})headers = {"x-api-key": anthropic_api_key,"anthropic-version": "2023-06-01","Content-Type": "application/json"}payload = {"model": "claude-3-5-sonnet-20241022","max_tokens": 1024,"messages": messages}try:async with httpx.AsyncClient() as client:logger.info(f"Sending Anthropic request: {payload}")response = await client.post("https://api.anthropic.com/v1/messages", headers=headers, json=payload)response.raise_for_status()logger.info(f"Anthropic response: {response.json()}")return response.json()['content'][0]['text']except httpx.HTTPStatusError as e:logger.error(f"Anthropic HTTP Status Error: {e.response.status_code}, {e.response.text}")return f"Error: Anthropic HTTP Status Error: {e.response.status_code}, {e.response.text}"except Exception as e:logger.error(f"Anthropic Error: {str(e)}")return f"Error: Anthropic Error: {str(e)}"async def ask_gemini(query: str, history: List[Dict[str, str]]) -> str:gemini_api_key = os.getenv("GEMINI_API_KEY")if not gemini_api_key:logger.error("Gemini API key not provided")return "Error: Gemini API key not provided."# Gemini doesn't natively support chat history in the same way, so we concatenate history as texthistory_text = ""for msg in history:history_text += f"User: {msg['user']}\nAssistant: {msg['bot']}\n" if msg["bot"] else f"User: {msg['user']}\n"full_query = history_text + f"User: {query}\n"headers = {"Content-Type": "application/json"}payload = {"contents": [{"parts": [{"text": full_query}]}]}try:async with httpx.AsyncClient() as client:response = await client.post(f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={gemini_api_key}",headers=headers,json=payload)response.raise_for_status()return response.json()['candidates'][0]['content']['parts'][0]['text']except httpx.HTTPStatusError as e:logger.error(f"Gemini HTTP Status Error: {e.response.status_code}, {e.response.text}")return f"Error: Gemini HTTP Status Error: {e.response.status_code}, {e.response.text}"except Exception as e:logger.error(f"Gemini Error: {str(e)}")return f"Error: Gemini Error: {str(e)}"async def query_model(query: str, provider: str, history: List[Dict[str, str]]) -> AsyncGenerator[Tuple[str, List[Dict[str, str]]], None]:provider = provider.lower()response = ""if provider == "openai":async for chunk in ask_openai(query, history):response += chunkyield chunk, history # Yield partial response for streamingelif provider == "anthropic":response = await ask_anthropic(query, history)yield response, historyelif provider == "gemini":response = await ask_gemini(query, history)yield response, historyelse:response = f"Error: Unknown provider: {provider}"yield response, history# Update history with the new query and responseupdated_history = history + [{"user": query, "bot": response}]logger.info(f"Updated history: {updated_history}")yield response, updated_history # Final yield with updated historyasync def submit_query(query: str, provider: str, history: List[Dict[str, str]]) -> AsyncGenerator[Tuple[str, List[Dict[str, str]], List[Dict[str, str]]], None]:if not query.strip():yield "", [{"role": "assistant", "content": "Please enter a query."}], historyreturnresponse = ""async for response_chunk, updated_history in query_model(query, provider, history):response += response_chunk# Convert history to chatbot messages formatchatbot_messages = []for msg in updated_history:chatbot_messages.append({"role": "user", "content": msg["user"]})if msg["bot"]:chatbot_messages.append({"role": "assistant", "content": msg["bot"]})if response and provider == "openai":# For streaming, show partial responsechatbot_messages[-1] = {"role": "assistant", "content": response}yield "", chatbot_messages, updated_history # Yield intermediate updates# Final yield with complete responsechatbot_messages = []for msg in updated_history:chatbot_messages.append({"role": "user", "content": msg["user"]})if msg["bot"]:chatbot_messages.append({"role": "assistant", "content": msg["bot"]})yield "", chatbot_messages, updated_history# Gradio interfacedef clear_history():return [], []with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# Multi-Model Chat")gr.Markdown("Chat with OpenAI, Anthropic, or Gemini. Select a provider and start typing!")provider = gr.Dropdown(choices=["OpenAI", "Anthropic", "Gemini"], label="Select Provider", value="OpenAI")history_state = gr.State(value=[])chatbot = gr.Chatbot(label="Conversation", type="messages")query = gr.Textbox(label="Enter your query", placeholder="e.g., What is the capital of the United States?")submit_button = gr.Button("Submit")clear_button = gr.Button("Clear History")submit_button.click(fn=submit_query,inputs=[query, provider, history_state],outputs=[query, chatbot, history_state])clear_button.click(fn=clear_history,inputs=[],outputs=[chatbot, history_state])# Launch the Gradio appdemo.launch()48 unmodified lines4950515253543 unmodified lines5859606162636412 unmodified lines77787980818283848586878848 unmodified linesresponse.raise_for_status()async for chunk in response.aiter_text():if chunk:lines = chunk.splitlines()for line in lines:if line.startswith("data: "):3 unmodified linesif not data.strip():continuetry:json_data = json.loads(data)if "choices" in json_data and json_data["choices"]:delta = json_data["choices"][0].get("delta", {})if "content" in delta and delta["content"] is not None:12 unmodified lineslogger.error(f"OpenAI Error: {str(e)}")yield f"Error: OpenAI Error: {str(e)}"async def ask_anthropic(query: str, history: List[Dict[str, str]]) -> AsyncGenerator[str, None]:# Previous implementation of ask_anthropic function (without streaming)# Other functions (ask_gemini, query_model, submit_query, clear_history) remain unchanged# Gradio interface (unchanged)# Launch the Gradio appdemo.launch()
app.py+5-154