Skip to content

Commit 966cc8c

Browse files
committed
Update assistant_id optional in the constructor, optionally function can be called on a object, assistant create & modify method introduced. Visible methods first in the class sequence.
1 parent 7fe7d55 commit 966cc8c

File tree

1 file changed

+143
-82
lines changed

1 file changed

+143
-82
lines changed

src/OpenAIAssistant.php

Lines changed: 143 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class OpenAIAssistant {
1414

1515
public function __construct(
1616
$api_key,
17-
$assistant_id,
17+
$assistant_id = null,
1818
$base_url = 'https://api.openai.com/v1',
1919
$version_header = 'OpenAI-Beta: assistants=v1'
2020
)
@@ -25,63 +25,64 @@ public function __construct(
2525
$this->version_header = $version_header;
2626
}
2727

28-
private function execute_request($ch)
28+
public function create_assistant($name, $instructions, $tools)
2929
{
30-
$response = curl_exec($ch);
31-
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
30+
$response = $this->send_post_request('/assistants', array(
31+
'name' => $name,
32+
'instructions' => $instructions,
33+
'model' => 'gpt-4-1106-preview',
34+
'tools' => $tools
35+
));
3236

33-
if ($errno = curl_errno($ch)) {
34-
throw new Exception(
35-
'CURL failed to call OpenAI API: ' . curl_error($ch),
36-
$errno
37-
);
38-
} else if ($http_code != 200) {
39-
throw new Exception(
40-
"OpenAI API Returned Unexpected HTTP code $http_code"
41-
);
37+
if (empty($response['id'])) {
38+
throw new Exception('Unable to create a assistant');
4239
}
43-
curl_close($ch);
44-
return json_decode($response, true);
40+
$this->assistant_id = $response['id'];
41+
return $response['id'];
4542
}
4643

47-
private function send_get_request($route)
44+
public function modify_assistant($name, $instructions, $tools)
4845
{
49-
$ch = curl_init();
50-
curl_setopt($ch, CURLOPT_URL, "{$this->base_url}{$route}");
51-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
52-
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
53-
"Authorization: Bearer {$this->api_key}",
54-
'Content-Type: application/json',
55-
'Accept: application/json',
56-
$this->version_header
46+
if (!$this->assistant_id) {
47+
throw new Exception(
48+
'You need to provide a assistant_id or create an assistant.'
49+
);
50+
}
51+
52+
$response = $this->send_post_request("/assistants/{$this->assistant_id}", array(
53+
'name' => $name,
54+
'instructions' => $instructions,
55+
'model' => 'gpt-4-1106-preview',
56+
'tools' => $tools
5757
));
58-
return $this->execute_request($ch);
58+
59+
if (empty($response['id'])) {
60+
throw new Exception('Unable to create a assistant');
61+
}
62+
$this->assistant_id = $response['id'];
63+
return $response['id'];
5964
}
6065

61-
private function send_post_request($route, $payload = null)
66+
public function list_assistants()
6267
{
63-
$ch = curl_init();
68+
$response = $this->send_get_request('/assistants');
6469

65-
if (!empty($payload)) curl_setopt(
66-
$ch,
67-
CURLOPT_POSTFIELDS,
68-
json_encode($payload)
69-
);
70-
curl_setopt($ch, CURLOPT_URL, "{$this->base_url}{$route}");
71-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
72-
curl_setopt($ch, CURLOPT_POST, true);
73-
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
74-
"Authorization: Bearer {$this->api_key}",
75-
'Content-Type: application/json',
76-
'Accept: application/json',
77-
$this->version_header
78-
));
79-
return $this->execute_request($ch);
70+
if (empty($response['data'])) {
71+
return array();
72+
}
73+
return $response['data'];
8074
}
8175

82-
public function create_thread()
76+
public function create_thread($content, $role = 'user')
8377
{
84-
$response = $this->send_post_request('/threads');
78+
$response = $this->send_post_request('/threads', array(
79+
'messages' => array(
80+
array(
81+
'role' => $role,
82+
'content' => $content
83+
)
84+
)
85+
));
8586

8687
if (empty($response['id'])) {
8788
throw new Exception('Unable to create a thread');
@@ -99,7 +100,7 @@ public function get_thread($thread_id)
99100
return $response;
100101
}
101102

102-
public function create_message($thread_id, $content, $role = 'user')
103+
public function add_message($thread_id, $content, $role = 'user')
103104
{
104105
// Check if any latest run requires_action
105106
// Before adding a new message to the thread
@@ -142,7 +143,7 @@ public function get_message($thread_id, $message_id)
142143
return $response;
143144
}
144145

145-
public function list_messages($thread_id)
146+
public function list_thread_messages($thread_id)
146147
{
147148
$response = $this->send_get_request("/threads/{$thread_id}/messages");
148149

@@ -152,39 +153,6 @@ public function list_messages($thread_id)
152153
return $response['data'];
153154
}
154155

155-
private function create_run($thread_id, $assistant_id)
156-
{
157-
$response = $this->send_post_request(
158-
"/threads/{$thread_id}/runs",
159-
array('assistant_id' => $assistant_id)
160-
);
161-
162-
if (empty($response['id'])) {
163-
throw new Exception('Unable to create a run');
164-
}
165-
return $response['id'];
166-
}
167-
168-
private function get_run($thread_id, $run_id)
169-
{
170-
$response = $this->send_get_request("/threads/{$thread_id}/runs/{$run_id}");
171-
172-
if (empty($response['id'])) {
173-
throw new Exception('Unable to create a run');
174-
}
175-
return $response;
176-
}
177-
178-
public function list_runs($thread_id)
179-
{
180-
$response = $this->send_get_request("/threads/{$thread_id}/runs");
181-
182-
if (empty($response['data'])) {
183-
return array();
184-
}
185-
return $response['data'];
186-
}
187-
188156
public function run_thread($thread_id)
189157
{
190158
// Check if any latest run requires_action
@@ -224,7 +192,11 @@ public function run_thread($thread_id)
224192
return false;
225193
}
226194

227-
public function execute_tools($thread_id, $execution_id)
195+
public function execute_tools(
196+
$thread_id,
197+
$execution_id,
198+
$optional_object = null
199+
)
228200
{
229201
$run = $this->get_run($thread_id, $execution_id);
230202
$calls = $run['required_action']['submit_tool_outputs']['tool_calls'];
@@ -233,10 +205,12 @@ public function execute_tools($thread_id, $execution_id)
233205
foreach ($calls as $call) {
234206
$method_name = $call['function']['name'];
235207
$method_args = json_decode($call['function']['arguments'], true);
208+
$callable = $optional_object ?
209+
array($optional_object, $method_name) : $method_name;
236210

237-
if (is_callable($method_name)) {
211+
if (is_callable($callable)) {
238212
$data = call_user_func_array(
239-
$method_name,
213+
$callable,
240214
$method_args
241215
);
242216
array_push($outputs, array(
@@ -279,4 +253,91 @@ public function submit_tool_outputs($thread_id, $execution_id, $outputs)
279253
}
280254
return false;
281255
}
256+
257+
private function execute_request($ch)
258+
{
259+
$response = curl_exec($ch);
260+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
261+
262+
if ($errno = curl_errno($ch)) {
263+
throw new Exception(
264+
'CURL failed to call OpenAI API: ' . curl_error($ch),
265+
$errno
266+
);
267+
} else if ($http_code != 200) {
268+
throw new Exception(
269+
"OpenAI API Returned Unexpected HTTP code $http_code"
270+
);
271+
}
272+
curl_close($ch);
273+
return json_decode($response, true);
274+
}
275+
276+
private function send_get_request($route)
277+
{
278+
$ch = curl_init();
279+
curl_setopt($ch, CURLOPT_URL, "{$this->base_url}{$route}");
280+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
281+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
282+
"Authorization: Bearer {$this->api_key}",
283+
'Content-Type: application/json',
284+
'Accept: application/json',
285+
$this->version_header
286+
));
287+
return $this->execute_request($ch);
288+
}
289+
290+
private function send_post_request($route, $payload = null)
291+
{
292+
$ch = curl_init();
293+
294+
if (!empty($payload)) curl_setopt(
295+
$ch,
296+
CURLOPT_POSTFIELDS,
297+
json_encode($payload)
298+
);
299+
curl_setopt($ch, CURLOPT_URL, "{$this->base_url}{$route}");
300+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
301+
curl_setopt($ch, CURLOPT_POST, true);
302+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
303+
"Authorization: Bearer {$this->api_key}",
304+
'Content-Type: application/json',
305+
'Accept: application/json',
306+
$this->version_header
307+
));
308+
return $this->execute_request($ch);
309+
}
310+
311+
private function create_run($thread_id, $assistant_id)
312+
{
313+
$response = $this->send_post_request(
314+
"/threads/{$thread_id}/runs",
315+
array('assistant_id' => $assistant_id)
316+
);
317+
318+
if (empty($response['id'])) {
319+
throw new Exception('Unable to create a run');
320+
}
321+
return $response['id'];
322+
}
323+
324+
private function get_run($thread_id, $run_id)
325+
{
326+
$response = $this->send_get_request("/threads/{$thread_id}/runs/{$run_id}");
327+
328+
if (empty($response['id'])) {
329+
throw new Exception('Unable to create a run');
330+
}
331+
return $response;
332+
}
333+
334+
private function list_runs($thread_id)
335+
{
336+
$response = $this->send_get_request("/threads/{$thread_id}/runs");
337+
338+
if (empty($response['data'])) {
339+
return array();
340+
}
341+
return $response['data'];
342+
}
282343
}

0 commit comments

Comments
 (0)