-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_openai_function_calling.py
More file actions
67 lines (51 loc) · 1.88 KB
/
Copy path03_openai_function_calling.py
File metadata and controls
67 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Use ToolsConnector with OpenAI function calling.
Full loop: generate tool schemas, send to OpenAI, execute the tool
call that comes back, and feed the result back into the conversation.
Prerequisites:
pip install "toolsconnector[github]" openai
export TC_GITHUB_CREDENTIALS='ghp_your-personal-access-token'
export OPENAI_API_KEY='sk-your-key'
"""
import json
import os
from openai import OpenAI
from toolsconnector.serve import ToolKit
# -- Setup --
client = OpenAI()
# Create a read-only ToolKit for GitHub.
# include_actions accepts glob patterns to whitelist specific actions.
kit = ToolKit(
connectors=["github"],
credentials={"github": os.environ.get("TC_GITHUB_CREDENTIALS", "")},
include_actions=["list_*", "get_*", "search_*"], # read-only subset
)
# Generate OpenAI-compatible tool definitions.
# These match the `tools` parameter format for chat.completions.create.
tools = kit.to_openai_tools()
print(f"Registered {len(tools)} tools with OpenAI")
# -- Chat with tool use --
messages = [
{"role": "user", "content": "List the 3 most recent repos for the 'anthropics' org"},
]
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
)
# -- Process tool calls --
for choice in response.choices:
message = choice.message
if message.tool_calls:
# The model wants to call one or more tools
for call in message.tool_calls:
print(f"\nCalling: {call.function.name}")
print(f"Args: {call.function.arguments}")
# Execute through ToolKit -- handles auth, retries, timeouts
result = kit.execute(
call.function.name,
json.loads(call.function.arguments),
)
print(f"Result: {result[:200]}...")
else:
# The model responded with text (no tool call needed)
print(f"Response: {message.content}")