|
import sys, os |
|
import traceback |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
import os, io |
|
|
|
sys.path.insert( |
|
0, os.path.abspath("../..") |
|
) |
|
import pytest |
|
import litellm |
|
from litellm import embedding, completion, completion_cost, Timeout |
|
from litellm import RateLimitError |
|
import pytest |
|
litellm.num_retries = 0 |
|
litellm.cache = None |
|
|
|
import json |
|
|
|
|
|
|
|
def get_current_weather(location, unit="fahrenheit"): |
|
"""Get the current weather in a given location""" |
|
if "tokyo" in location.lower(): |
|
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": "celsius"}) |
|
elif "san francisco" in location.lower(): |
|
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"}) |
|
elif "paris" in location.lower(): |
|
return json.dumps({"location": "Paris", "temperature": "22", "unit": "celsius"}) |
|
else: |
|
return json.dumps({"location": location, "temperature": "unknown"}) |
|
|
|
|
|
|
|
def test_parallel_function_call(): |
|
try: |
|
|
|
messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}] |
|
tools = [ |
|
{ |
|
"type": "function", |
|
"function": { |
|
"name": "get_current_weather", |
|
"description": "Get the current weather in a given location", |
|
"parameters": { |
|
"type": "object", |
|
"properties": { |
|
"location": { |
|
"type": "string", |
|
"description": "The city and state, e.g. San Francisco, CA", |
|
}, |
|
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, |
|
}, |
|
"required": ["location"], |
|
}, |
|
}, |
|
} |
|
] |
|
response = litellm.completion( |
|
model="gpt-3.5-turbo-1106", |
|
messages=messages, |
|
tools=tools, |
|
tool_choice="auto", |
|
) |
|
print("Response\n", response) |
|
response_message = response.choices[0].message |
|
tool_calls = response_message.tool_calls |
|
|
|
print("length of tool calls", len(tool_calls)) |
|
print("Expecting there to be 3 tool calls") |
|
assert len(tool_calls) > 1 |
|
|
|
|
|
if tool_calls: |
|
|
|
|
|
available_functions = { |
|
"get_current_weather": get_current_weather, |
|
} |
|
messages.append(response_message) |
|
print("Response message\n", response_message) |
|
|
|
for tool_call in tool_calls: |
|
function_name = tool_call.function.name |
|
function_to_call = available_functions[function_name] |
|
function_args = json.loads(tool_call.function.arguments) |
|
function_response = function_to_call( |
|
location=function_args.get("location"), |
|
unit=function_args.get("unit"), |
|
) |
|
messages.append( |
|
{ |
|
"tool_call_id": tool_call.id, |
|
"role": "tool", |
|
"name": function_name, |
|
"content": function_response, |
|
} |
|
) |
|
print(f"messages: {messages}") |
|
second_response = litellm.completion( |
|
model="gpt-3.5-turbo-1106", |
|
messages=messages, |
|
temperature=0.2, |
|
seed=22 |
|
) |
|
print("second response\n", second_response) |
|
return second_response |
|
except Exception as e: |
|
pytest.fail(f"Error occurred: {e}") |
|
|
|
test_parallel_function_call() |
|
|
|
|
|
|
|
|
|
def test_parallel_function_call_stream(): |
|
try: |
|
|
|
messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}] |
|
tools = [ |
|
{ |
|
"type": "function", |
|
"function": { |
|
"name": "get_current_weather", |
|
"description": "Get the current weather in a given location", |
|
"parameters": { |
|
"type": "object", |
|
"properties": { |
|
"location": { |
|
"type": "string", |
|
"description": "The city and state, e.g. San Francisco, CA", |
|
}, |
|
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, |
|
}, |
|
"required": ["location"], |
|
}, |
|
}, |
|
} |
|
] |
|
response = litellm.completion( |
|
model="gpt-3.5-turbo-1106", |
|
messages=messages, |
|
tools=tools, |
|
stream=True, |
|
tool_choice="auto", |
|
complete_response = True |
|
) |
|
print("Response\n", response) |
|
|
|
|
|
response_message = response.choices[0].message |
|
tool_calls = response_message.tool_calls |
|
|
|
print("length of tool calls", len(tool_calls)) |
|
print("Expecting there to be 3 tool calls") |
|
assert len(tool_calls) > 1 |
|
|
|
|
|
if tool_calls: |
|
|
|
|
|
available_functions = { |
|
"get_current_weather": get_current_weather, |
|
} |
|
messages.append(response_message) |
|
print("Response message\n", response_message) |
|
|
|
for tool_call in tool_calls: |
|
function_name = tool_call.function.name |
|
function_to_call = available_functions[function_name] |
|
function_args = json.loads(tool_call.function.arguments) |
|
function_response = function_to_call( |
|
location=function_args.get("location"), |
|
unit=function_args.get("unit"), |
|
) |
|
messages.append( |
|
{ |
|
"tool_call_id": tool_call.id, |
|
"role": "tool", |
|
"name": function_name, |
|
"content": function_response, |
|
} |
|
) |
|
print(f"messages: {messages}") |
|
second_response = litellm.completion( |
|
model="gpt-3.5-turbo-1106", |
|
messages=messages, |
|
temperature=0.2, |
|
seed=22 |
|
) |
|
print("second response\n", second_response) |
|
return second_response |
|
except Exception as e: |
|
pytest.fail(f"Error occurred: {e}") |
|
|
|
test_parallel_function_call_stream() |