-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
116 lines (104 loc) · 3.64 KB
/
code.power
File metadata and controls
116 lines (104 loc) · 3.64 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
/**
* Create a chat completion with the OpenAI API.
* API Ref: https://platform.openai.com/docs/api-reference/chat/create
*
* @param string $model The model to use for completion.
* @param array $messages A list of messages describing the conversation so far.
*
* Each item in the array is an object with the following:
* - role (string) Required
* The role of the author of this message.
* One of system, user, or assistant.
* - content (string) Required
* The contents of the message.
* - name (string) Optional
* The name of the author of this message.
* May contain a-z, A-Z, 0-9, and underscores,
* with a maximum length of 64 characters.
*
* @param int|null $maxTokens Maximum number of tokens to generate (optional).
* @param float|null $temperature The sampling temperature to use (optional).
* @param float|null $topP The nucleus sampling parameter (optional).
* @param int|null $n The number of chat completion choices to generate (optional).
* @param bool|null $stream Partial message deltas (optional).
* @param mixed|null $stop Sequences where the API will stop generating tokens (optional).
* @param float|null $presencePenalty Penalty for new tokens based on whether they appear in the text (optional).
* @param float|null $frequencyPenalty Penalty for new tokens based on their frequency in the text (optional).
* @param array|null $logitBias Modify the likelihood of specified tokens appearing (optional).
* @param string|null $user A unique identifier representing the end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function create(
string $model,
array $messages,
?int $maxTokens = null,
?float $temperature = null,
?float $topP = null,
?int $n = null,
?bool $stream = null,
$stop = null,
?float $presencePenalty = null,
?float $frequencyPenalty = null,
?array $logitBias = null,
?string $user = null
): ?object
{
// Build the request path.
$path = "/chat/completions";
// Set the request data.
$data = new \stdClass();
$data->model = $model;
$data->messages = $messages;
if ($maxTokens !== null)
{
$data->max_tokens = $maxTokens;
}
if ($temperature !== null)
{
$data->temperature = $temperature;
}
if ($topP !== null)
{
$data->top_p = $topP;
}
if ($n !== null)
{
$data->n = $n;
}
if ($stream !== null)
{
$data->stream = $stream;
}
if ($stop !== null)
{
$data->stop = $stop;
}
if ($presencePenalty !== null)
{
$data->presence_penalty = $presencePenalty;
}
if ($frequencyPenalty !== null)
{
$data->frequency_penalty = $frequencyPenalty;
}
if ($logitBias !== null)
{
$data->logit_bias = new \stdClass();
foreach ($logitBias as $key => $val)
{
$data->logit_bias->$key = $val;
}
}
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}