llama-factory
57 строк · 2.2 Кб
1import json
2from typing import Sequence
3
4from openai import OpenAI
5from transformers.utils.versions import require_version
6
7
8require_version("openai>=1.5.0", "To fix: pip install openai>=1.5.0")
9
10
11def calculate_gpa(grades: Sequence[str], hours: Sequence[int]) -> float:
12grade_to_score = {"A": 4, "B": 3, "C": 2}
13total_score, total_hour = 0, 0
14for grade, hour in zip(grades, hours):
15total_score += grade_to_score[grade] * hour
16total_hour += hour
17return total_score / total_hour
18
19
20tool_map = {"calculate_gpa": calculate_gpa}
21
22
23if __name__ == "__main__":
24client = OpenAI(
25api_key="0",
26base_url="http://localhost:8000/v1",
27)
28tools = [
29{
30"type": "function",
31"function": {
32"name": "calculate_gpa",
33"description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
34"parameters": {
35"type": "object",
36"properties": {
37"grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
38"hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
39},
40"required": ["grades", "hours"],
41},
42},
43}
44]
45messages = []
46messages.append({"role": "user", "content": "My grades are A, A, B, and C. The credit hours are 3, 4, 3, and 2."})
47result = client.chat.completions.create(messages=messages, model="test", tools=tools)
48tool_call = result.choices[0].message.tool_calls[0].function
49name, arguments = tool_call.name, json.loads(tool_call.arguments)
50messages.append(
51{"role": "function", "content": json.dumps({"name": name, "argument": arguments}, ensure_ascii=False)}
52)
53tool_result = tool_map[name](**arguments)
54messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)})
55result = client.chat.completions.create(messages=messages, model="test", tools=tools)
56print(result.choices[0].message.content)
57# Based on your grades and credit hours, your calculated Grade Point Average (GPA) is 3.4166666666666665.
58