mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 19:54:13 +00:00
test(test_function_calling.py): handle for when model returns a text response
This commit is contained in:
parent
caa46ca905
commit
10d65f0676
1 changed files with 41 additions and 38 deletions
|
@ -266,47 +266,50 @@ def test_groq_parallel_function_call():
|
||||||
)
|
)
|
||||||
print("Response\n", response)
|
print("Response\n", response)
|
||||||
response_message = response.choices[0].message
|
response_message = response.choices[0].message
|
||||||
tool_calls = response_message.tool_calls
|
if hasattr(response_message, "tool_calls"):
|
||||||
|
tool_calls = response_message.tool_calls
|
||||||
|
|
||||||
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
|
assert isinstance(
|
||||||
assert isinstance(
|
response.choices[0].message.tool_calls[0].function.name, str
|
||||||
response.choices[0].message.tool_calls[0].function.arguments, str
|
)
|
||||||
)
|
assert isinstance(
|
||||||
|
response.choices[0].message.tool_calls[0].function.arguments, str
|
||||||
|
)
|
||||||
|
|
||||||
print("length of tool calls", len(tool_calls))
|
print("length of tool calls", len(tool_calls))
|
||||||
|
|
||||||
# Step 2: check if the model wanted to call a function
|
# Step 2: check if the model wanted to call a function
|
||||||
if tool_calls:
|
if tool_calls:
|
||||||
# Step 3: call the function
|
# Step 3: call the function
|
||||||
# Note: the JSON response may not always be valid; be sure to handle errors
|
# Note: the JSON response may not always be valid; be sure to handle errors
|
||||||
available_functions = {
|
available_functions = {
|
||||||
"get_current_weather": get_current_weather,
|
"get_current_weather": get_current_weather,
|
||||||
} # only one function in this example, but you can have multiple
|
} # only one function in this example, but you can have multiple
|
||||||
messages.append(
|
|
||||||
response_message
|
|
||||||
) # extend conversation with assistant's reply
|
|
||||||
print("Response message\n", response_message)
|
|
||||||
# Step 4: send the info for each function call and function response to the model
|
|
||||||
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(
|
messages.append(
|
||||||
{
|
response_message
|
||||||
"tool_call_id": tool_call.id,
|
) # extend conversation with assistant's reply
|
||||||
"role": "tool",
|
print("Response message\n", response_message)
|
||||||
"name": function_name,
|
# Step 4: send the info for each function call and function response to the model
|
||||||
"content": function_response,
|
for tool_call in tool_calls:
|
||||||
}
|
function_name = tool_call.function.name
|
||||||
) # extend conversation with function response
|
function_to_call = available_functions[function_name]
|
||||||
print(f"messages: {messages}")
|
function_args = json.loads(tool_call.function.arguments)
|
||||||
second_response = litellm.completion(
|
function_response = function_to_call(
|
||||||
model="groq/llama2-70b-4096", messages=messages
|
location=function_args.get("location"),
|
||||||
) # get a new response from the model where it can see the function response
|
unit=function_args.get("unit"),
|
||||||
print("second response\n", second_response)
|
)
|
||||||
|
messages.append(
|
||||||
|
{
|
||||||
|
"tool_call_id": tool_call.id,
|
||||||
|
"role": "tool",
|
||||||
|
"name": function_name,
|
||||||
|
"content": function_response,
|
||||||
|
}
|
||||||
|
) # extend conversation with function response
|
||||||
|
print(f"messages: {messages}")
|
||||||
|
second_response = litellm.completion(
|
||||||
|
model="groq/llama2-70b-4096", messages=messages
|
||||||
|
) # get a new response from the model where it can see the function response
|
||||||
|
print("second response\n", second_response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pytest.fail(f"Error occurred: {e}")
|
pytest.fail(f"Error occurred: {e}")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue