Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Tool Call
  • Loading branch information
jamesrochabrun authored Nov 15, 2023
1 parent e63c1be commit 0d8a76c
Showing 1 changed file with 46 additions and 91 deletions.
137 changes: 46 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,33 +442,6 @@ public struct ChatCompletionParameters: Encodable {
case tool // content, role, tool_call_id
}

public struct ToolCall: Encodable {
/// The ID of the tool call.
let id: String
/// The type of the tool. Currently, only `function` is supported.
let type: String
/// The function that the model called.
let function: FunctionCall

public init(
id: String,
type: String = "function",
function: FunctionCall)
{
self.id = id
self.type = type
self.function = function
}
}

public struct FunctionCall: Encodable {

/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
let arguments: String
/// The name of the function to call.
let name: String
}

enum CodingKeys: String, CodingKey {
case role
case content
Expand Down Expand Up @@ -853,10 +826,7 @@ public struct ChatCompletionObject: Decodable {
/// The index of the choice in the list of choices.
public let index: Int
/// A chat completion message generated by the model.
public let message: ChatMessage

// Vision

public let message: ChatMessage

public struct ChatMessage: Decodable {

Expand All @@ -872,32 +842,6 @@ public struct ChatCompletionObject: Decodable {
/// Provided by the Vision API.
public let finishDetails: FinishDetails?

public struct ToolCall: Decodable {
/// The ID of the tool call.
public let id: String?
/// The type of the tool. Currently, only `function` is supported.
public let type: String?
/// The function that the model called.
public let function: FunctionCall

public init(
id: String,
type: String = "function",
function: FunctionCall)
{
self.id = id
self.type = type
self.function = function
}
}

public struct FunctionCall: Decodable {
/// The name of the function to call.
public let name: String
/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
public let arguments: String
}

/// Provided by the Vision API.
public struct FinishDetails: Decodable {
let type: String
Expand Down Expand Up @@ -997,37 +941,6 @@ public struct ChatCompletionChunkObject: Decodable {
/// The role of the author of this message.
public let role: String?

public struct ToolCall: Decodable {

public let index: Int
/// The ID of the tool call.
public let id: String?
/// The type of the tool. Currently, only `function` is supported.
public let type: String?
/// The function that the model called.
public let function: FunctionCall

public init(
index: Int,
id: String,
type: String = "function",
function: FunctionCall)
{
self.index = index
self.id = id
self.type = type
self.function = function
}
}

public struct FunctionCall: Decodable {

/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
public let arguments: String
/// The name of the function to call.
public let name: String?
}

enum CodingKeys: String, CodingKey {
case content
case toolCalls = "tool_calls"
Expand Down Expand Up @@ -1070,10 +983,51 @@ let chatCompletionObject = try await service.startStreamedChat(parameters: param

Chat Completion also supports [Function Calling](https://platform.openai.com/docs/guides/function-calling) and [Parallel Function Calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling). `functions` has been deprecated in favor of `tools` check [OpenAI Documentation](https://platform.openai.com/docs/api-reference/chat/create) for more.

```swift
public struct ToolCall: Codable {

public let index: Int
/// The ID of the tool call.
public let id: String?
/// The type of the tool. Currently, only `function` is supported.
public let type: String?
/// The function that the model called.
public let function: FunctionCall

public init(
index: Int,
id: String,
type: String = "function",
function: FunctionCall)
{
self.index = index
self.id = id
self.type = type
self.function = function
}
}

public struct FunctionCall: Codable {

/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
let arguments: String
/// The name of the function to call.
let name: String

public init(
arguments: String,
name: String)
{
self.arguments = arguments
self.name = name
}
}
```

Usage
```swift
/// Define a `ChatCompletionParameters.Tool`
var tool: ChatCompletionParameters.Tool {
/// Define a `ToolCall`
var tool: ToolCall {
.init(
type: "function", // The type of the tool. Currently, only "function" is supported.
function: .init(
Expand All @@ -1089,7 +1043,8 @@ var tool: ChatCompletionParameters.Tool {
}

let prompt = "Show me an image of an unicorn eating ice cream"
let parameters = ChatCompletionParameters(messages: [.init(role: .user, content: .text(prompt))], model: .gpt41106Preview, tools: [tool])
let content: ChatCompletionParameters.Message.ContentType = .text(prompt)
let parameters = ChatCompletionParameters(messages: [.init(role: .user, content: content)], model: .gpt41106Preview, tools: [tool])
let chatCompletionObject = try await service.startStreamedChat(parameters: parameters)
```
For more details about how to also uploadin base 64 encoded images in iOS check the [ChatFunctionsCalllDemo](https://github.com/jamesrochabrun/SwiftOpenAI/tree/main/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall) demo on the Examples section of this package.
Expand Down

0 comments on commit 0d8a76c

Please sign in to comment.