Skip to content

Commit a8aceaf

Browse files
committed
Chat function completion example added to README
1 parent 82ab1d5 commit a8aceaf

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Examples:
170170
println(completion.choices.head.text)
171171
).runWith(Sink.ignore)
172172
```
173-
(For this to work you need to use `OpenAIServiceStreamedFactory` from `openai-scala-client-stream` lib)
173+
For this to work you need to use `OpenAIServiceStreamedFactory` from `openai-scala-client-stream` lib.
174174

175175
- Create chat completion
176176

@@ -194,6 +194,54 @@ Examples:
194194
}
195195
```
196196

197+
- Create chat completion for functions (🔥 new)
198+
199+
```scala
200+
val messages = Seq(
201+
FunMessageSpec(role = ChatRole.User, content = Some("What's the weather like in Boston?")),
202+
)
203+
204+
// as a param type we can use "number", "string", "boolean", "object", "array", and "null"
205+
val functions = Seq(
206+
FunctionSpec(
207+
name = "get_current_weather",
208+
description = Some("Get the current weather in a given location"),
209+
parameters = Map(
210+
"type" -> "object",
211+
"properties" -> Map(
212+
"location" -> Map(
213+
"type" -> "string",
214+
"description" -> "The city and state, e.g. San Francisco, CA",
215+
),
216+
"unit" -> Map(
217+
"type" -> "string",
218+
"enum" -> Seq("celsius", "fahrenheit")
219+
)
220+
),
221+
"required" -> Seq("location"),
222+
)
223+
)
224+
)
225+
226+
// if we want to force the model to use the above function as a response
227+
// we can do so by passing: responseFunctionName = Some("set_current_location")`
228+
service.createChatFunCompletion(
229+
messages = messages,
230+
functions = functions,
231+
responseFunctionName = None
232+
).map { response =>
233+
val chatFunCompletionMessage = response.choices.head.message
234+
val functionCall = chatFunCompletionMessage.function_call
235+
236+
println("function call name : " + functionCall.map(_.name).getOrElse("N/A"))
237+
println("function call arguments : " + functionCall.map(_.arguments).getOrElse("N/A"))
238+
}
239+
```
240+
Note that instead of `MessageSpec`, the `function_call` version of the chat completion uses the `FunMessageSpec` class to define messages - both as part of the request and the response.
241+
This extension of the standard chat completion is currently supported by the following `0613` models, all conveniently available in `ModelId` object:
242+
- `gpt-3.5-turbo-0613` (default), `gpt-3.5-turbo-16k-0613`, `gpt-4-0613`, and `gpt-4-32k-0613`.
243+
244+
197245
**✔️ Important Note**: After you are done using the service, you should close it by calling `service.close`. Otherwise, the underlying resources/threads won't be released.
198246

199247
**III. Using multiple services (🔥 new)**

0 commit comments

Comments
 (0)