update tool test
This commit is contained in:
parent
f51d48ea08
commit
37ec5deaec
|
@ -1,46 +1,55 @@
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
from typing import Sequence
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
|
|
||||||
os.environ["OPENAI_BASE_URL"] = "http://192.168.5.193:8000/v1"
|
os.environ["OPENAI_BASE_URL"] = "http://192.168.0.1:8000/v1"
|
||||||
os.environ["OPENAI_API_KEY"] = "0"
|
os.environ["OPENAI_API_KEY"] = "0"
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_gpa(grades: Sequence[str], hours: Sequence[int]) -> float:
|
||||||
|
grade_to_score = {"A": 4, "B": 3, "C": 2}
|
||||||
|
total_score, total_hour = 0, 0
|
||||||
|
for grade, hour in zip(grades, hours):
|
||||||
|
total_score += grade_to_score[grade] * hour
|
||||||
|
total_hour += hour
|
||||||
|
return total_score / total_hour
|
||||||
|
|
||||||
|
|
||||||
|
tool_map = {"calculate_gpa": calculate_gpa}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
client = OpenAI()
|
client = OpenAI()
|
||||||
tools = [
|
tools = [
|
||||||
{
|
{
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "get_current_weather",
|
"name": "calculate_gpa",
|
||||||
"description": "Get the current weather in a given location",
|
"description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}
|
"grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
|
||||||
|
"hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
|
||||||
},
|
},
|
||||||
"required": ["location"],
|
"required": ["grades", "hours"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
result = client.chat.completions.create(
|
messages = []
|
||||||
messages=[{"role": "user", "content": "What is the weather like in Boston?"}],
|
messages.append({"role": "user", "content": "My grades are A, A, B, and C. The credit hours are 3, 4, 3, and 2."})
|
||||||
model="gpt-3.5-turbo",
|
result = client.chat.completions.create(messages=messages, model="test", tools=tools)
|
||||||
tools=tools,
|
tool_call = result.choices[0].message.tool_calls[0].function
|
||||||
|
name, arguments = tool_call.name, json.loads(tool_call.arguments)
|
||||||
|
messages.append(
|
||||||
|
{"role": "function", "content": json.dumps({"name": name, "argument": arguments}, ensure_ascii=False)}
|
||||||
)
|
)
|
||||||
print(result.choices[0].message)
|
tool_result = tool_map[name](**arguments)
|
||||||
result = client.chat.completions.create(
|
messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)})
|
||||||
messages=[
|
result = client.chat.completions.create(messages=messages, model="test", tools=tools)
|
||||||
{"role": "user", "content": "What is the weather like in Boston?"},
|
print(result.choices[0].message.content)
|
||||||
{
|
# Based on your grades and credit hours, your calculated Grade Point Average (GPA) is 3.4166666666666665.
|
||||||
"role": "function",
|
|
||||||
"content": """{"name": "get_current_weather", "arguments": {"location": "Boston, MA"}}""",
|
|
||||||
},
|
|
||||||
{"role": "tool", "content": '{"temperature": 22, "unit": "celsius", "description": "Sunny"}'},
|
|
||||||
],
|
|
||||||
model="gpt-3.5-turbo",
|
|
||||||
tools=tools,
|
|
||||||
)
|
|
||||||
print(result.choices[0].message)
|
|
||||||
|
|
Loading…
Reference in New Issue