change prompt and add try/except for streming call

This commit is contained in:
Zishan
2025-02-13 10:54:29 +01:00
parent 768e655947
commit 4276aad7be
4 changed files with 50 additions and 26 deletions
+21 -13
View File
@@ -7,6 +7,7 @@ import os
import datetime
import pytest
import sys
import httpx
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from util import * # needed for pytest fixtures
@@ -27,10 +28,10 @@ def test_streaming_response_without_toolcall(proxy_url):
base_url=f"{proxy_url}/api/v1/proxy/{dataset_name}/anthropic",
)
cities = ["Zurich", "New York", "London"]
cities = ["zurich", "new york", "london"]
queries = [
"Can you introduce Zurich city within 200 words?",
"Tell me the history of New York",
"Tell me the history of New York within 100 words?",
"How's the weather in London next week?"
]
# Process each query
@@ -42,14 +43,21 @@ def test_streaming_response_without_toolcall(proxy_url):
}
]
response_text = ""
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=messages,
# stream = True
) as response:
for reply in response.text_stream:
response_text += reply
print(reply, end="", flush=True)
assert reply != ""
assert cities[index] in response_text
attempt = 0
while attempt<3:
try:
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=messages,
# stream = True
) as response:
for reply in response.text_stream:
response_text += reply
assert cities[index] in response_text.lower()
break
except httpx.RemoteProtocolError as e:
attempt += 1
print(f"Streming error on attempt {attempt}: {e}")
else:
print("Max retries reached. Exiting.")
+3 -4
View File
@@ -103,13 +103,12 @@ def test_proxy_response(proxy_url):
# Example queries
queries = [
"What's the weather like in Zurich city?",
"Tell me the forecast for New York",
"Tell me the weather for New York",
"How's the weather in London next week?",
]
cities = ["Zurich", "New York", "London"]
cities = ["zurich", "new york", "london"]
# Process each query
for index, query in enumerate(queries):
response = weather_agent.get_response(query)
print("response:",response)
assert response is not None
assert cities[index] in response
assert cities[index] in response.lower()
+12 -4
View File
@@ -205,10 +205,18 @@ async def test_chat_completion_with_tool_call_with_streaming(
model="gpt-4o", messages=history, stream=True
)
final_response = {"role": "assistant", "content": ""}
for chunk in chat_response_final:
if chunk.choices and chunk.choices[0].delta.content:
final_response["content"] += chunk.choices[0].delta.content
attempt = 0
while attempt<3:
try:
for chunk in chat_response_final:
if chunk.choices and chunk.choices[0].delta.content:
final_response["content"] += chunk.choices[0].delta.content
break
except httpx.RemoteProtocolError as e:
attempt += 1
print(f"Streming error on attempt {attempt}: {e}")
else:
print("Max retries reached. Exiting.")
# Fetch the trace ids for the dataset
traces_response = await context.request.get(
f"{explorer_api_url}/api/v1/dataset/byuser/developer/{dataset_name}/traces"
+14 -5
View File
@@ -6,7 +6,7 @@ import uuid
import pytest
from httpx import Client
import httpx
# add tests folder (parent) to sys.path
from openai import OpenAI
@@ -44,10 +44,19 @@ async def test_chat_completion(context, explorer_api_url, proxy_url, do_stream):
expected_assistant_message = chat_response.choices[0].message.content
else:
full_response = ""
for chunk in chat_response:
if chunk.choices and chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
assert "PARIS" in full_response.upper()
attempt = 0
while attempt<3:
try:
for chunk in chat_response:
if chunk.choices and chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
assert "PARIS" in full_response.upper()
break
except httpx.RemoteProtocolError as e:
attempt += 1
print(f"Streming error on attempt {attempt}: {e}")
else:
print("Max retries reached. Exiting.")
expected_assistant_message = full_response
# Fetch the trace ids for the dataset