-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Chat Completion Function Call options
- Loading branch information
1 parent
ede4fc3
commit 8553a29
Showing
22 changed files
with
535 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
Examples/iOS_Example/iOS_Example/Chat/CreateChatFunctionCallExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// CreateChatFunctionCallExample.swift | ||
// iOS_Example | ||
// | ||
// Created by Marcus Arnett on 10/9/23. | ||
// | ||
|
||
import SwiftUI | ||
import OpenAIKit | ||
|
||
struct CreateChatFunctionCallExample: View { | ||
@State private var isCompleting: Bool = false | ||
@State private var weatherInfo: WeatherInfo? = nil | ||
|
||
enum TemperatureUnit: String, Codable { | ||
case fahrenheit | ||
case celsius | ||
} | ||
|
||
struct WeatherInfo: Codable { | ||
let location: String | ||
let temperature: String | ||
let unit: TemperatureUnit | ||
let forecast: [String] | ||
} | ||
|
||
func getCurrentWeather(location: String, unit: TemperatureUnit = .fahrenheit) -> WeatherInfo { | ||
return WeatherInfo(location: location, temperature: "72", unit: unit, forecast: ["sunny", "windy"]) | ||
} | ||
|
||
let messages: [ChatMessage] = [ChatMessage(role: .user, content: "What's the weather like in Boston?")] | ||
|
||
let functions: [Function] = [ | ||
Function( | ||
name: "getCurrentWeather", | ||
description: "Get the current weather in a given location", | ||
parameters: Parameters( | ||
type: "object", | ||
properties: [ | ||
"location": ParameterDetail(type: "string", description: "The city and state, e.g. San Francisco, CA"), | ||
"unit": ParameterDetail(type: "string", enumValues: ["fahrenheit", "celsius"]) | ||
], | ||
required: ["location"] | ||
) | ||
) | ||
] | ||
|
||
var body: some View { | ||
VStack { | ||
VStack(alignment: .leading) { | ||
ForEach(messages) { message in | ||
Text("\(message.role.rawValue.capitalized): \(message.content ?? "NO CONTENT")") | ||
.padding(.vertical, 10) | ||
} | ||
} | ||
.padding(20) | ||
|
||
if isCompleting { | ||
VStack { | ||
if let weatherInfo { | ||
Text("Assistant: The current weather in \(weatherInfo.location) is \(weatherInfo.temperature) degrees \(weatherInfo.unit.rawValue).") | ||
} else { | ||
Text("Assistant: ...") | ||
} | ||
} | ||
.padding() | ||
} else { | ||
VStack { | ||
Button { | ||
isCompleting = true | ||
|
||
Task { | ||
do { | ||
let config = Configuration( | ||
organizationId: "INSERT-ORGANIZATION-ID", | ||
apiKey: "INSERT-API-KEY" | ||
) | ||
let openAI = OpenAI(config) | ||
let chatParameters = ChatParameters( | ||
model: .chatGPTTurbo, | ||
messages: messages, | ||
functionCall: "auto", | ||
functions: functions | ||
) | ||
let chatCompletion = try await openAI.generateChatCompletion( | ||
parameters: chatParameters | ||
) | ||
|
||
if let message = chatCompletion.choices[0].message, let functionCall = message.functionCall { | ||
let jsonString = functionCall.arguments | ||
if let data = jsonString.data(using: .utf8) { | ||
do { | ||
if | ||
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], | ||
let location = json["location"] as? String | ||
{ | ||
self.weatherInfo = self.getCurrentWeather(location: location) | ||
} | ||
} catch { | ||
print("Error parsing JSON: \(error)") | ||
} | ||
} | ||
} | ||
} catch { | ||
print("ERROR DETAILS - \(error)") | ||
} | ||
} | ||
} label: { | ||
Text("Generate Completion") | ||
.font(.headline) | ||
.foregroundColor(.white) | ||
.frame(width: 270, height: 50) | ||
.background(.blue) | ||
.clipShape(Capsule()) | ||
.padding(.top, 8) | ||
} | ||
} | ||
} | ||
} | ||
|
||
Spacer() | ||
} | ||
} | ||
|
||
struct CreateChatFunctionCallExample_Previews: PreviewProvider { | ||
static var previews: some View { | ||
NavigationStack { | ||
CreateChatFunctionCallExample() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// SwiftUIView.swift | ||
// | ||
// | ||
// Created by Marcus Arnett on 10/10/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
internal extension [String: Property] { | ||
var body: [String: Any] { | ||
var result: [String: Any] = [:] | ||
for key in self.keys { | ||
let value = self[key] | ||
if let value { | ||
result[key] = value.body | ||
} | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Property.swift | ||
// OpenAIKit | ||
// | ||
// Copyright (c) 2023 MarcoDotIO | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
/// The properties used for a function call | ||
public protocol Property: Codable { | ||
/// The type used for the property | ||
var type: String { get } | ||
var body: [String: Any] { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.