| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | client = OpenAI( |
| 36 | api_key="{}".format(os.getenv("API_KEY", "0")), |
| 37 | base_url="http://localhost:{}/v1".format(os.getenv("API_PORT", 8000)), |
| 38 | ) |
| 39 | tools = [ |
| 40 | { |
| 41 | "type": "function", |
| 42 | "function": { |
| 43 | "name": "calculate_gpa", |
| 44 | "description": "Calculate the Grade Point Average (GPA) based on grades and credit hours", |
| 45 | "parameters": { |
| 46 | "type": "object", |
| 47 | "properties": { |
| 48 | "grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"}, |
| 49 | "hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"}, |
| 50 | }, |
| 51 | "required": ["grades", "hours"], |
| 52 | }, |
| 53 | }, |
| 54 | } |
| 55 | ] |
| 56 | tool_map = {"calculate_gpa": calculate_gpa} |
| 57 | |
| 58 | messages = [] |
| 59 | messages.append({"role": "user", "content": "My grades are A, A, B, and C. The credit hours are 3, 4, 3, and 2."}) |
| 60 | result = client.chat.completions.create(messages=messages, model="test", tools=tools) |
| 61 | if result.choices[0].message.tool_calls is None: |
| 62 | raise ValueError("Cannot retrieve function call from the response.") |
| 63 | |
| 64 | messages.append(result.choices[0].message) |
| 65 | tool_call = result.choices[0].message.tool_calls[0].function |
| 66 | print(tool_call) |
| 67 | # Function(arguments='{"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}', name='calculate_gpa') |
| 68 | name, arguments = tool_call.name, json.loads(tool_call.arguments) |
| 69 | tool_result = tool_map[name](**arguments) |
| 70 | messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)}) |
| 71 | result = client.chat.completions.create(messages=messages, model="test", tools=tools) |
| 72 | print(result.choices[0].message.content) |
| 73 | # Based on the grades and credit hours you provided, your Grade Point Average (GPA) is 3.42. |
| 74 | |
| 75 | |
| 76 | if __name__ == "__main__": |