-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathchatbot_with_assistants_api.php
More file actions
130 lines (103 loc) · 3.22 KB
/
chatbot_with_assistants_api.php
File metadata and controls
130 lines (103 loc) · 3.22 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
<?php
// include library
require_once( "library/ChatGPT.php" );
require_once( "library/Assistant.php" );
require_once( "library/Thread.php" );
require_once( "library/Run.php" );
// get api key
$api_key = getenv( "OPENAI_API_KEY" );
// global cart array
$cart = [];
###############################################
# FUNCTIONS FOR CHATGPT #
###############################################
/**
* Gets the current weather information
*
* @param string $location Location for which to get the weather information
*/
function get_current_weather( $location ) {
echo "DEBUG: Getting current weather\n";
return "The weather is nice and sunny";
}
/**
* Adds a product to cart
*
* @param string $product Name of the product
* @param int $quantity Quantity to add to cart
*/
function add_to_cart( $product, $quantity ) {
global $cart;
if( ! isset( $cart[$product] ) ) {
$cart[$product] = 0;
}
$cart[$product] += $quantity;
echo "DEBUG: Added product '$product' to cart.\n";
return "Added product '$product' to cart.";
}
/**
* Get the current products in the shopping cart
*
* @param string $param Always set to 'cart'
*/
function get_cart_contents( $param ) {
global $cart;
if( count( $cart ) == 0 ) {
echo "DEBUG: Cart is empty\n";
return "The cart is empty.";
}
$contents = ["cart" => []];
foreach( $cart as $product => $quantity ) {
$contents["cart"][] = [
"name" => $product,
"quantity" => $quantity,
];
}
echo "DEBUG: Cart contents: " . json_encode( $contents ) . "\n";
return "Cart contents: " . json_encode( $contents );
}
###############################################
# TERMINAL CHATBOT IMPLEMENTATION #
###############################################
// initialize library
$chatgpt = new ChatGPT( $api_key );
$chatgpt->assistant_mode( true );
// create an assistant
$assistant = $chatgpt->create_assistant(
name: "Library Test",
model: "gpt-3.5-turbo-1106",
// give system message to assistant
instructions: "You are a chatbot on an online store. You can add products to cart by specifying a product name and a quantity to add. You can also get the current contents of the cart with the get_cart_contents function. You can provide the user with the contents of the cart when they ask",
// give functions to assistant
functions: [
"get_current_weather",
"add_to_cart",
"get_cart_contents",
]
);
// use created assistant
$chatgpt->set_assistant( $assistant );
// create a thread
$thread = $chatgpt->create_thread();
// use created thread
$chatgpt->set_thread( $thread );
// print info
echo "Assistant ID: " . $assistant->get_id() . "\n";
echo "Thread ID: " . $thread->get_id() . "\n\n";
// ask for user message
echo "ChatGPT: How can I assist you today?\n";
echo "You: ";
$prompt = fgets( fopen( "php://stdin", "r" ) );
echo "\n";
// chat loop
while( true ) {
// send user message
$chatgpt->umessage( $prompt );
// get response from ChatGPT
$message = $chatgpt->response()->content;
// ask for user message
echo "ChatGPT: ".$message."\n";
echo "You: ";
$prompt = fgets( fopen( "php://stdin", "r" ) );
echo "\n";
}