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
| use Laravel\OpenAI\Facades\OpenAI;
class AssistantService { protected $tools = []; public function __construct() { $this->registerTools(); } protected function registerTools() { $this->tools = [ [ 'name' => 'get_weather', 'description' => 'Get current weather for a location', 'parameters' => [ 'type' => 'object', 'properties' => [ 'location' => [ 'type' => 'string', 'description' => 'City and country, e.g., London, UK', ], ], 'required' => ['location'], ], ], [ 'name' => 'search_web', 'description' => 'Search the web for recent information', 'parameters' => [ 'type' => 'object', 'properties' => [ 'query' => [ 'type' => 'string', 'description' => 'Search query', ], ], 'required' => ['query'], ], ], [ 'name' => 'calculate', 'description' => 'Perform mathematical calculations', 'parameters' => [ 'type' => 'object', 'properties' => [ 'expression' => [ 'type' => 'string', 'description' => 'Mathematical expression, e.g., 2 + 2 * 3', ], ], 'required' => ['expression'], ], ], ]; } public function processRequest(string $request) { $messages = [ ['role' => 'system', 'content' => 'You are a helpful assistant with access to tools. Use them when needed.'], ['role' => 'user', 'content' => $request], ]; return $this->processMessages($messages); } protected function processMessages(array $messages) { $response = OpenAI::chat()->create([ 'model' => 'gpt-4o', 'messages' => $messages, 'functions' => $this->tools, 'function_call' => 'auto', ]); $message = $response->choices[0]->message; if (isset($message->function_call)) { $functionName = $message->function_call->name; $arguments = json_decode($message->function_call->arguments, true); $functionResult = $this->executeFunction($functionName, $arguments); $messages[] = $message; $messages[] = [ 'role' => 'function', 'name' => $functionName, 'content' => json_encode($functionResult), ]; return $this->processMessages($messages); } else { return $message->content; } } protected function executeFunction(string $functionName, array $arguments) { switch ($functionName) { case 'get_weather': return $this->getWeather($arguments['location']); case 'search_web': return $this->searchWeb($arguments['query']); case 'calculate': return $this->calculate($arguments['expression']); default: throw new \Exception("Unknown function: {$functionName}"); } } protected function getWeather(string $location) { return [ 'location' => $location, 'temperature' => 22, 'condition' => 'Sunny', 'humidity' => 65, 'wind_speed' => 10, ]; } protected function searchWeb(string $query) { return [ 'query' => $query, 'results' => [ ['title' => 'Result 1', 'snippet' => 'This is a search result'], ['title' => 'Result 2', 'snippet' => 'This is another result'], ], ]; } protected function calculate(string $expression) { try { return ['result' => eval("return {$expression};")]; } catch (\Exception $e) { return ['error' => 'Invalid expression']; } } }
|