Skip to content

Commit fbd8369

Browse files
Add: documentation for openAIChat
1 parent 8a75d29 commit fbd8369

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed

doc/functions/openAIChat.md

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
2+
# openAIChat
3+
4+
Connect to OpenAI® Chat Completion API from MATLAB®
5+
6+
# Creation
7+
## Syntax
8+
9+
`chat = openAIChat`
10+
11+
12+
`chat = openAIChat(systemPrompt)`
13+
14+
15+
`chat = openAIChat(___,ApiKey=key)`
16+
17+
18+
`chat = openAIChat(___,Name=Value)`
19+
20+
## Description
21+
22+
Connect to the OpenAI Chat Completion API to generate text using large language models developed by OpenAI.
23+
24+
25+
To connect to the OpenAI API, you need a valid API key. For information on how to obtain an API key, see [https://platform.openai.com/docs/quickstart](https://platform.openai.com/docs/quickstart).
26+
27+
28+
`chat = openAIChat` creates an `openAIChat` object. Connecting to the OpenAI API requires a valid API key. Either set the environment variable `OPENAI_API_KEY` or specify the `APIKey` name\-value argument.
29+
30+
31+
`chat = openAIChat(``systemPrompt``)` creates an `openAIChat` object with the specified system prompt.
32+
33+
34+
`chat = openAIChat(___,``APIKey``=key)` uses the specified API key.
35+
36+
37+
`chat = openAIChat(___,``Name=Value``)` specifies additional options using one or more name\-value arguments.
38+
39+
# Input Arguments
40+
### `systemPrompt` — System prompt
41+
42+
character vector | string scalar
43+
44+
45+
Specify the system prompt and set the `SystemPrompt` property. The system prompt is a natural\-language description that provides the framework in which a large language model generates its responses. The system prompt can include instructions about tone, communications style, language, etc.
46+
47+
48+
**Example**: "You are a helpful assistant who provides answers to user queries in iambic pentameter."
49+
50+
## Name\-Value Arguments
51+
### `APIKey` — OpenAI API key
52+
53+
character vector | string scalar
54+
55+
56+
OpenAI API key to access OpenAI APIs such as ChatGPT.
57+
58+
59+
Instead of using the `APIKey` name\-value argument, you can also set the environment variable OPEN\_API\_KEY. For more information, see [OpenAI API](http://../OpenAI.md).
60+
61+
### `Tools` — OpenAI functions to use during output generation
62+
63+
`openAIFunction` object | array of `openAIFunction` objects
64+
65+
66+
Custom functions used by the model to collect or generate additional data.
67+
68+
69+
For an example, see [Analyze Scientific Papers Using ChatGPT Function Calls](http://../examples/AnalyzeScientificPapersUsingFunctionCalls.md).
70+
71+
# Properties Settable at Construction
72+
73+
Optionally specify these properties at construction using name\-value arguments. Specify `PropertyName1=PropertyValue1,...,PropertyNameN=PropertyValueN`, where `PropertyName` is the property name and `PropertyValue` is the corresponding value.
74+
75+
### `ModelName` — Model name
76+
77+
`"gpt-4o-mini"` (default) | `"gpt-4"` | `"gpt-3.5-turbo"` | `"dall-e-2"` | ...
78+
79+
80+
Name of the OpenAI model to use for text or image generation.
81+
82+
83+
For a list of currently supported models, see [OpenAI API](../OpenAI.md).
84+
85+
### `Temperature` — Temperature
86+
87+
`1` (default) | numeric scalar between `0` and `2`
88+
89+
90+
Temperature value for controlling the randomness of the output. Higher temperature increases the randomness of the output. Setting the temperature to `0` results in fully deterministic output.
91+
92+
### `TopP` — Top probability mass
93+
94+
`1` (default) | numeric scalar between `0` and `1`
95+
96+
97+
Top probability mass for controlling the diversity of the generated output. Higher top probability mass corresponds to higher diversity.
98+
99+
### `StopSequences` — Stop sequences
100+
101+
`[]` (default) | string array with between `0` and `4` elements
102+
103+
104+
Sequences that stop generation of tokens.
105+
106+
107+
**Example:** `["The end.","And that is all she wrote."]`
108+
109+
### `PresencePenalty` — Presence penalty
110+
111+
`0` (default) | numeric scalar between `-2` and `2`
112+
113+
114+
Penalty value for using a token that has already been used at least once in the generated output. Higher values reduce the repetition of tokens. Negative values increase the repetition of tokens.
115+
116+
117+
The presence penalty is independent of the number of incidents of a token, so long as it has been used at least once. To increase the penalty for every additional time a token is generated, use the `FrequencyPenalty` name\-value argument.
118+
119+
### `FrequencyPenalty` — Frequency penalty
120+
121+
`0` (default) | numeric scalar between `-2` and `2`
122+
123+
124+
Penalty value for repeatedly using the same token in the generated output. Higher values reduce the repetition of tokens. Negative values increase the repetition of tokens.
125+
126+
127+
The frequency penalty increases with every instance of a token in the generated output. To use a constant penalty for a repeated token, independent of the number of instances that token is generated, use the `PresencePenalty` name\-value argument.
128+
129+
### `TimeOut` — Connection timeout in seconds
130+
131+
`10` (default) | nonnegative numeric scalar
132+
133+
134+
After construction, this property is read\-only.
135+
136+
137+
If the OpenAI server does not respond within the timeout, then the function throws an error.
138+
139+
### `StreamFun` — Custom streaming function
140+
141+
function handle
142+
143+
144+
Specify a custom streaming function to process the generated output token by token as it is being generated, rather than having to wait for the end of the generation. For example, you can use this function to print the output as it is generated.
145+
146+
147+
For an example, see [Process Generated Text in Real Time by Using ChatGPT™ in Streaming Mode](../../examples/ProcessGeneratedTextinRealTimebyUsingChatGPTinStreamingMode.md).
148+
149+
150+
**Example:** `@(token) fprint("%s",token)`
151+
152+
### `ResponseFormat` — Response format
153+
154+
`"text"` (default) | `"json"`
155+
156+
157+
After construction, this property is read\-only.
158+
159+
160+
Format of generated output.
161+
162+
163+
If you set the response format to `"text"`, then the generated output is a string.
164+
165+
166+
If you set the response format to `"json"`, then the generated output is a string containing JSON encoded data.
167+
168+
169+
To configure the format of the generated JSON file, describe the format using natural language and provide it to the model either in the system prompt or as a user message. The prompt or message describing the format must contain the word `"json"` or `"JSON"`.
170+
171+
172+
For an example, see [Analyze Sentiment in Text Using ChatGPT in JSON Mode](../../examples/AnalyzeSentimentinTextUsingChatGPTinJSONMode.md).
173+
174+
175+
The JSON response format is not supported for these models:
176+
177+
- `ModelName="gpt-4"`
178+
- `ModelName="gpt-4-0613"`
179+
# Other Properties
180+
### `SystemPrompt` — System prompt
181+
182+
character vector | string scalar
183+
184+
185+
This property is read\-only.
186+
187+
188+
The system prompt is a natural\-language description that provides the framework in which a large language model generates its responses. The system prompt can include instructions about tone, communications style, language, etc.
189+
190+
191+
**Example**: "You are a helpful assistant who provides answers to user queries in iambic pentameter."
192+
193+
### `FunctionNames` — Names of OpenAI functions to use during output generation
194+
195+
string array
196+
197+
198+
This property is read\-only.
199+
200+
201+
Names of the custom functions specified in the `Tools` name\-value argument.
202+
203+
# Object Functions
204+
205+
[`generate`](generate.md) — Generate output from large language models
206+
207+
# Examples
208+
## Create OpenAI Chat
209+
```matlab
210+
loadenv(".env")
211+
modelName = "gpt-4o-mini";
212+
chat = openAIChat("You are a helpful assistant awaiting further instructions.",ModelName=modelName)
213+
```
214+
215+
```matlabTextOutput
216+
chat =
217+
openAIChat with properties:
218+
219+
ModelName: "gpt-4o-mini"
220+
Temperature: 1
221+
TopP: 1
222+
StopSequences: [0x0 string]
223+
TimeOut: 10
224+
SystemPrompt: {[1x1 struct]}
225+
ResponseFormat: "text"
226+
PresencePenalty: 0
227+
FrequencyPenalty: 0
228+
FunctionNames: []
229+
230+
```
231+
## Generate and Stream Text
232+
```matlab
233+
loadenv(".env")
234+
sf = @(x) fprintf("%s",x);
235+
chat = openAIChat(StreamFun=sf);
236+
generate(chat,"Why is a raven like a writing desk?",MaxNumTokens=50)
237+
```
238+
239+
```matlabTextOutput
240+
The phrase "Why is a raven like a writing desk?" comes from Lewis Carroll's "Alice's Adventures in Wonderland." Initially posed by the Mad Hatter during the tea party scene, the question is often interpreted as nonsense, in line with the book
241+
ans = "The phrase "Why is a raven like a writing desk?" comes from Lewis Carroll's "Alice's Adventures in Wonderland." Initially posed by the Mad Hatter during the tea party scene, the question is often interpreted as nonsense, in line with the book"
242+
```
243+
# See Also
244+
- [Create Simple Chat Bot](../../examples/CreateSimpleChatBot.md)
245+
- [Process Generated Text in Real Time Using ChatGPT in Streaming Mode](../../examples/ProcessGeneratedTextinRealTimebyUsingChatGPTinStreamingMode.md)
246+
- [Analyze Scientific Papers Using Function Calls](../../examples/AnalyzeScientificPapersUsingFunctionCalls.md)
247+
- [Analyze Sentiment in Text Using ChatGPT in JSON Mode](../../examples/AnalyzeSentimentinTextUsingChatGPTinJSONMode.md)
248+
249+
*Copyright 2024 The MathWorks, Inc.*
250+

0 commit comments

Comments
 (0)