This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathfunction-calling.py
More file actions
173 lines (167 loc) · 5.98 KB
/
function-calling.py
File metadata and controls
173 lines (167 loc) · 5.98 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from datetime import datetime
from openai import OpenAI
from pydantic import BaseModel
import json
# MODEL = "deepseek-r1-distill-qwen-7b:7b"
MODEL = "llama3.1:8b-q8"
client = OpenAI(
base_url="http://localhost:39281/v1",
api_key="not-needed", # Authentication is not required for local deployment
)
tools = [
{
"type": "function",
"function": {
"name": "puppeteer_navigate",
"description": "Navigate to a URL",
"parameters": {
"properties": {"url": {"type": "string"}},
"required": ["url"],
"type": "object",
},
"strict": False,
},
},
{
"type": "function",
"function": {
"name": "puppeteer_screenshot",
"description": "Take a screenshot of the current page or a specific element",
"parameters": {
"properties": {
"height": {
"description": "Height in pixels (default: 600)",
"type": "number",
},
"name": {
"description": "Name for the screenshot",
"type": "string",
},
"selector": {
"description": "CSS selector for element to screenshot",
"type": "string",
},
"width": {
"description": "Width in pixels (default: 800)",
"type": "number",
},
},
"required": ["name"],
"type": "object",
},
"strict": False,
},
},
{
"type": "function",
"function": {
"name": "puppeteer_click",
"description": "Click an element on the page",
"parameters": {
"properties": {
"selector": {
"description": "CSS selector for element to click",
"type": "string",
}
},
"required": ["selector"],
"type": "object",
},
"strict": False,
},
},
{
"type": "function",
"function": {
"name": "puppeteer_fill",
"description": "Fill out an input field",
"parameters": {
"properties": {
"selector": {
"description": "CSS selector for input field",
"type": "string",
},
"value": {"description": "Value to fill", "type": "string"},
},
"required": ["selector", "value"],
"type": "object",
},
"strict": False,
},
},
{
"type": "function",
"function": {
"name": "puppeteer_select",
"description": "Select an element on the page with Select tag",
"parameters": {
"properties": {
"selector": {
"description": "CSS selector for element to select",
"type": "string",
},
"value": {"description": "Value to select", "type": "string"},
},
"required": ["selector", "value"],
"type": "object",
},
"strict": False,
},
},
{
"type": "function",
"function": {
"name": "puppeteer_hover",
"description": "Hover an element on the page",
"parameters": {
"properties": {
"selector": {
"description": "CSS selector for element to hover",
"type": "string",
}
},
"required": ["selector"],
"type": "object",
},
"strict": False,
},
},
{
"type": "function",
"function": {
"name": "puppeteer_evaluate",
"description": "Execute JavaScript in the browser console",
"parameters": {
"properties": {
"script": {
"description": "JavaScript code to execute",
"type": "string",
}
},
"required": ["script"],
"type": "object",
},
"strict": False,
},
},
]
completion_payload = {
"messages": [
{
"role": "system",
"content": 'You have access to the following CUSTOM functions:\n\n<CUSTOM_FUNCTIONS>\n\nIf a you choose to call a function ONLY reply in the following format:\n<{start_tag}={function_name}>{parameters}{end_tag}\nwhere\n\nstart_tag => `<function`\nparameters => a JSON dict with the function argument name as key and function argument value as value.\nend_tag => `</function>`\n\nHere is an example,\n<function=example_function_name>{"example_name": "example_value"}</function>\n\nReminder:\n- Function calls MUST follow the specified format\n- Required parameters MUST be specified\n- You can call one or more functions at a time, but remember only chose correct function\n- Put the entire function call reply on one line\n- Always add your sources when using search results to answer the user query\n- If you can not find correct parameters or arguments corresponding to function in the user\'s message, ask user again to provide, do not make assumptions.\n- No explanation are needed when calling a function.\n\nYou are a helpful assistant.',
},
{
"role": "user",
"content": "go to google search",
},
]
}
response = client.chat.completions.create(
top_p=0.9,
temperature=0.6,
model=MODEL,
messages=completion_payload["messages"],
tools=tools,
)
print(response)