2024-01-21 11:41:46 +00:00
|
|
|
import json
|
2024-03-07 12:26:31 +00:00
|
|
|
import os
|
2024-01-21 11:41:46 +00:00
|
|
|
from typing import Sequence
|
2024-01-21 11:15:27 +00:00
|
|
|
|
|
|
|
from openai import OpenAI
|
2024-02-05 14:50:43 +00:00
|
|
|
from transformers.utils.versions import require_version
|
2024-01-21 11:15:27 +00:00
|
|
|
|
2024-02-05 14:51:03 +00:00
|
|
|
|
2024-02-05 14:50:43 +00:00
|
|
|
require_version("openai>=1.5.0", "To fix: pip install openai>=1.5.0")
|
2024-01-21 11:15:27 +00:00
|
|
|
|
|
|
|
|
2024-01-21 11:41:46 +00:00
|
|
|
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
|
2024-04-01 13:35:18 +00:00
|
|
|
return round(total_score / total_hour, 2)
|
2024-01-21 11:41:46 +00:00
|
|
|
|
|
|
|
|
2024-03-07 12:26:31 +00:00
|
|
|
def main():
|
2024-02-14 18:27:36 +00:00
|
|
|
client = OpenAI(
|
|
|
|
api_key="0",
|
2024-03-07 12:26:31 +00:00
|
|
|
base_url="http://localhost:{}/v1".format(os.environ.get("API_PORT", 8000)),
|
2024-02-14 18:27:36 +00:00
|
|
|
)
|
2024-01-21 11:15:27 +00:00
|
|
|
tools = [
|
|
|
|
{
|
|
|
|
"type": "function",
|
|
|
|
"function": {
|
2024-01-21 11:41:46 +00:00
|
|
|
"name": "calculate_gpa",
|
|
|
|
"description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
|
2024-01-21 11:15:27 +00:00
|
|
|
"parameters": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2024-01-21 11:41:46 +00:00
|
|
|
"grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
|
|
|
|
"hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
|
2024-01-21 11:15:27 +00:00
|
|
|
},
|
2024-01-21 11:41:46 +00:00
|
|
|
"required": ["grades", "hours"],
|
2024-01-21 11:15:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
2024-03-07 12:26:31 +00:00
|
|
|
tool_map = {"calculate_gpa": calculate_gpa}
|
|
|
|
|
2024-01-21 11:41:46 +00:00
|
|
|
messages = []
|
|
|
|
messages.append({"role": "user", "content": "My grades are A, A, B, and C. The credit hours are 3, 4, 3, and 2."})
|
|
|
|
result = client.chat.completions.create(messages=messages, model="test", tools=tools)
|
2024-04-01 13:35:18 +00:00
|
|
|
if result.choices[0].message.tool_calls is None:
|
|
|
|
raise ValueError("Cannot retrieve function call from the response.")
|
|
|
|
|
|
|
|
messages.append(result.choices[0].message)
|
2024-01-21 11:41:46 +00:00
|
|
|
tool_call = result.choices[0].message.tool_calls[0].function
|
2024-04-01 13:35:18 +00:00
|
|
|
print(tool_call)
|
|
|
|
# Function(arguments='{"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}', name='calculate_gpa')
|
2024-01-21 11:41:46 +00:00
|
|
|
name, arguments = tool_call.name, json.loads(tool_call.arguments)
|
|
|
|
tool_result = tool_map[name](**arguments)
|
|
|
|
messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)})
|
|
|
|
result = client.chat.completions.create(messages=messages, model="test", tools=tools)
|
|
|
|
print(result.choices[0].message.content)
|
2024-04-01 13:35:18 +00:00
|
|
|
# Based on the grades and credit hours you provided, your Grade Point Average (GPA) is 3.42.
|
2024-03-07 12:26:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|