-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathchatbot_with_library.php
105 lines (82 loc) · 2.62 KB
/
chatbot_with_library.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
<?php
// include library
require_once( "library/ChatGPT.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 ) {
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 );
// set system message
$chatgpt->smessage( "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" );
// add functions
$chatgpt->add_function( "get_current_weather" );
$chatgpt->add_function( "add_to_cart" );
$chatgpt->add_function( "get_cart_contents" );
// 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";
}