.gitignore000064400000000052144761607060006544 0ustar00/vendor/ /tests/ composer.lock .vscode/ README.md000064400000000011144761607060006026 0ustar00# openai composer.json000064400000000676144761607060007312 0ustar00{ "name": "boru/openai", "type": "library", "autoload": { "psr-4": { "boru\\openai\\": "src/" } }, "authors": [ { "name": "Daniel Hayes", "email": "dhayes@boruapps.com" } ], "require": { "boru/dhutils": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] } src/OpenAI.php000064400000011537144761607060007201 0ustar00header("Authorization","Bearer " . static::$api_key); $req->header("Content-Type","application/json"); $req->header("Accept","application/json"); $req->header("User-Agent","OpenAI-PHP/0.1"); $req->header("OpenAI-Source","boru/openai-php"); } /** * @param mixed $type * @param string $path * @param array $parameters * @return array|ApiResponse|false * @throws Exception */ public static function request($type,$path="",$parameters=[],$asArray=false) { static::verifyInit(); $request = static::$http->request($type,static::getEndpoint($path)); static::authHeaders($request); if(!empty($parameters) && is_array($parameters)) { $request->body(json_encode($parameters)); } if($asArray) { ApiResponse::fromResponse($request->send())->asArray(); } return ApiResponse::fromResponse($request->send()); } /** * @param string $path * @param array $paramaters * @return \boru\dhutils\http\Request * @throws \Exception */ public static function post($path="",$paramaters=[]) { static::verifyInit(); $request = static::$http->request("post",static::getEndpoint($path)); static::authHeaders($request); if(!empty($paramaters) && is_array($paramaters)) { $request->body(json_encode($paramaters)); } return $request; } /** * @param string $path * @param array $paramaters * @return \boru\dhutils\http\Request * @throws \Exception */ public static function get($path="",$paramaters=[]) { static::verifyInit(); $request = static::$http->request("get",static::getEndpoint($path)); static::authHeaders($request); if(!empty($paramaters) && is_array($paramaters)) { $request->body(json_encode($paramaters)); } return $request; } /** * @param string $path * @param array $paramaters * @return \boru\dhutils\http\Request * @throws \Exception */ public static function put($path="",$paramaters=[]) { static::verifyInit(); $request = static::$http->request("put",static::getEndpoint($path)); static::authHeaders($request); if(!empty($paramaters) && is_array($paramaters)) { $request->body(json_encode($paramaters)); } return $request; } /** * @param string $path * @param array $paramaters * @return \boru\dhutils\http\Request * @throws \Exception */ public static function delete($path="",$paramaters=[]) { static::verifyInit(); $request = static::$http->request("delete",static::getEndpoint($path)); static::authHeaders($request); if(!empty($paramaters) && is_array($paramaters)) { $request->body(json_encode($paramaters)); } return $request; } //Convinience functions public static function listModels() { return static::request("get","models"); } public static function getModel($model_id) { return static::request("get","models/" . $model_id); } public static function chat($options=[]) { $chat = Chat::fromArray($options); return $chat->send(); } }src/api/Chat.php000064400000014421144761607060007511 0ustar00model = $model; } /** * @param array $parameters * @return array|ApiResponse * @throws \Exception */ public static function create($parameters,$returnAsArray=false) { if($returnAsArray) { return OpenAI::request("post","chat/completions",$parameters,$returnAsArray); } else { $request = OpenAI::post("chat/completions",$parameters,$returnAsArray); return new ChatResponse($request->send()); } } /** * @return array|ApiResponse|false */ public function send($returnAsArray=false) { $parameters["model"] = $this->model; if(!is_null($this->temperature)) { $parameters["temperature"] = $this->temperature; } if(!is_null($this->top_p)) { $parameters["top_p"] = $this->top_p; } if(!is_null($this->max_tokens)) { $parameters["max_tokens"] = $this->max_tokens; } $parameters["n"] = $this->n; $parameters["stream"] = $this->stream; if(!empty($this->stop)) { $parameters["stop"] = $this->stop; } $parameters["presence_penalty"] = $this->presence_penalty; $parameters["frequency_penalty"] = $this->frequency_penalty; $parameters["messages"] = $this->messages; return self::create($parameters,$returnAsArray); } /** * Set the model to use for the chat */ public function setModel($model) { $this->model = $model; } public function setTemperature($temperature) { $this->temperature = $temperature; } public function setTopP($top_p) { $this->top_p = $top_p; } public function setMaxTokens($max_tokens) { $this->max_tokens = $max_tokens; } public function setN($n) { $this->n = $n; } public function setStream($stream) { $this->stream = $stream; } public function setStop($stop) { $this->stop = $stop; } public function setPresencePenalty($presence_penalty) { $this->presence_penalty = $presence_penalty; } public function setFrequencyPenalty($frequency_penalty) { $this->frequency_penalty = $frequency_penalty; } public function addMessage($role,$content,$name="") { $msg = [ "role" => $role, "content" => $content, ]; if($name) { $msg["name"] = $name; } $this->messages[] = $msg; } public static function fromArray($options=[]) { $chat = new Chat(); if(isset($options["model"])) { $chat->setModel($options["model"]); } if(isset($options["temperature"])) { $chat->setTemperature($options["temperature"]); } if(isset($options["top_p"])) { $chat->setTopP($options["top_p"]); } if(isset($options["max_tokens"])) { $chat->setMaxTokens($options["max_tokens"]); } if(isset($options["n"])) { $chat->setN($options["n"]); } if(isset($options["stream"])) { $chat->setStream($options["stream"]); } if(isset($options["stop"])) { $chat->setStop($options["stop"]); } if(isset($options["presence_penalty"])) { $chat->setPresencePenalty($options["presence_penalty"]); } if(isset($options["frequency_penalty"])) { $chat->setFrequencyPenalty($options["frequency_penalty"]); } if(isset($options["messages"])) { foreach($options["messages"] as $msg) { $chat->addMessage($msg["role"],$msg["content"],isset($msg["name"]) ? $msg["name"] : ""); } } return $chat; } }src/api/Completions.php000064400000000313144761607060011121 0ustar00setGetSetMainArray("body"); $this->response = $response; $this->body = $response->body(true); } public function asArray() { return $this->body; } public function __set($field,$val) { $this->set($field,$val); } /** * @param \boru\dhutils\http\Response $response */ public static function fromResponse($resp) { if($resp instanceof \boru\dhutils\http\Response) { return new self($resp); } return false; } /** * Inherits trait functions from GetSetArray * ->get($key,$default=null) * ->set($key,$val='',$append=false) * ->exists($key=null) */ public function jsonSerialize($array=null) { if(is_null($array)) { $array = $this->body; } return $array; } /** * Ignore intelephense error, php 5.6 :( */ public function offsetExists($offset) { return $this->exists($offset); } /** * Ignore intelephense error, php 5.6 :( */ public function offsetGet($offset) { return $this->get($offset); } /** * Ignore intelephense error, php 5.6 :( */ public function offsetSet($offset , $value) { $this->set($offset,$value); } /** * Ignore intelephense error, php 5.6 :( */ public function offsetUnset($offset) { $this->remove($this->$offset); } public function __toString() { return json_encode($this); } }src/api/responses/ChatResponse.php000064400000002033144761607060013245 0ustar00get("id",false); } public function object() { return $this->get("object",false); } public function created() { return $this->get("created",false); } public function model() { return $this->get("model",false); } public function usage() { return $this->get("usage",false); } public function usagePrompt() { return $this->get("usage.prompt_tokens",0); } public function usageCompletion() { return $this->get("usage.completion_tokens",0); } public function usageTotal() { return $this->get("usage.total_tokens",0); } public function choices() { return $this->get("choices",false); } public function choice($k=0) { if(isset($this->body["choices"][$k])) { return $this->body["choices"][$k]; } return false; } }src/tools/Template.php000064400000002217144761607060010774 0ustar00setTemplateFormat($templateFormat); } } public function setTemplateFormat($templateFormat) { $this->templateFormat = $templateFormat; } /** * Parse an array into a template * @param array $array * @return string */ public function parse($array) { $template = $this->templateFormat; foreach($array as $key => $value) { $template = str_replace("{{" . $key . "}}",$value,$template); } return $template; } /** * Parse an array of arrays into a template * @param array $array * @param string $split * @return string */ public function multiParse($array,$split="\n") { $string = ""; foreach($array as $arr) { if($string != "") { $string .= $split; } $string .= $this->parse($arr); } return $string; } }