-
Notifications
You must be signed in to change notification settings - Fork 0
/
console-chat-example.php
144 lines (118 loc) · 4.43 KB
/
console-chat-example.php
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
<?php
require "../vendor/autoload.php";
use Dragonzap\OpenAI\ChatGPT\APIConfiguration;
use Dragonzap\OpenAI\ChatGPT\Assistant;
use Dragonzap\OpenAI\ChatGPT\ConversationIdentificationData;
use Dragonzap\OpenAI\ChatGPT\Exceptions\ThreadRunResponseLastError;
/**
* Run the console chat
* php ./console-chat-example.php
*
* Start typing questions and get answers
*/
class JessicaAssistant extends Assistant
{
public function __construct($api_config = NULL)
{
parent::__construct($api_config);
}
/**
* You should replace the assistant ID with your own chatgpt assistant id.
*/
public function getAssistantId(): string
{
return 'asst_0q46BUiesPu5XStGHufJVCba';
}
/**
* This function is invoked automatically everytime the library wants us to save the conversation data
* to our database. This only has to be implemented if you plan to store conversations over multiple requests
* In this example we handle it all in a console application so we dont need to maintain any state.
*/
public function saveConversationIdentificationData(ConversationIdentificationData $conversation_id_data): void
{
// Since we are blocking we dont need to save this identification data
// the whole conversation is handled in one request.
}
private function handleGetWeatherFunction(array $arguments)
{
$success = false;
$message = 'We could not locate the weather for ' . $arguments['location'] . ' as it is not in our database';
switch(strtolower($arguments['location']))
{
case 'cardiff':
$success = true;
$message = 'The weather in wales, cardiff is Rainy today';
break;
case 'london':
$success = false;
$message = 'As usual england is freezing';
break;
case 'perth':
$success = false;
$message = 'Australia, Perth is very hot at 45 Celcius everyone is cooking';
break;
}
return [
'success' => $success,
'message' => $message,
];
}
public function handleFunction(string $function, array $arguments): string|array
{
$response = [];
switch($function)
{
case 'get_weather':
$response = $this->handleGetWeatherFunction($arguments);
break;
default:
$response = [
'success' => false,
'message' => 'Unknown function'
];
}
return $response;
}
}
// Replace the API Key with your own chatgpt API key
$assistant = new JessicaAssistant(new APIConfiguration('API KEY HERE'));
$conversation = $assistant->newConversation();
while(1)
{
$input_message = fgets(STDIN);
echo 'User:' . $input_message . "\n";
$conversation->sendMessage($input_message);
try
{
$conversation->blockUntilResponded();
}
catch(ThreadRunResponseLastError $ex) {
throw $ex;
}
echo 'Assistant: ' . $conversation->getResponseData()->getResponse(). "\n";
echo "[FUNCTION CALLS MADE]";
print_r($conversation->getResponseData()->getFunctionCalls());
// The code when run outputs the message chatgpt replied along with the function calls to get the result
// as can be seen in the response ChatGPT called our get_weather function defined in the JessicaAssistant class.
// php ./console-chat-example.php
// Whats the tempature today?
// User:Whats the tempature today?
// Assistant: Beep bop! Weather data malfunction for San Francisco, CA. Unable to provide the current temperature. Beep boop, consider checking a local weather service.
// [FUNCTION CALLS MADE]Array
// (
// [0] => Dragonzap\OpenAI\ChatGPT\GPTFunctionCall Object
// (
// [function_name:protected] => get_weather
// [function_arguments:protected] => Array
// (
// [location] => San Francisco, CA
// [unit] => f
// )
// [response:protected] => Array
// (
// [success] =>
// [message] => We could not locate the weather for San Francisco, CA as it is not in our database
// )
// )
// )
}