|
1 | 1 | # FunctionCalling-AIProxySwift |
2 | | -Easy to use your Swift native functions for function calling with AIProxySwift. |
| 2 | + |
| 3 | +This library simplifies the integration of the [FunctionCalling](https://github.com/fumito-ito/FunctionCalling) macro into [AIProxySwift](https://github.com/lzell/AIProxySwift). By using this library, you can directly generate `Tool` objects from Swift native functions, which can then be specified as FunctionCalling when invoking AIProxy. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```swift |
| 8 | + |
| 9 | +import FunctionCalling |
| 10 | +import FunctionCalling_AIProxySwift |
| 11 | +import AIProxy |
| 12 | + |
| 13 | +// MARK: Declare the container and functions for the tools to be called from FunctionCalling. |
| 14 | + |
| 15 | +@FunctionCalling(service: .chatGPT) |
| 16 | +struct MyFunctionTools { |
| 17 | + @CallableFunction |
| 18 | + /// Get the current stock price for a given ticker symbol |
| 19 | + /// |
| 20 | + /// - Parameter: The stock ticker symbol, e.g. GOOG for Google Inc. |
| 21 | + func getStockPrice(ticker: String) async throws -> String { |
| 22 | + // code to return stock price of passed ticker |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// MARK: You can directly pass the tools generated from objects to the model in AIProxy. |
| 27 | + |
| 28 | +let openAIService = AIProxy.openAIService( |
| 29 | + partialKey: "partial-key-from-your-developer-dashboard", |
| 30 | + serviceURL: "service-url-from-your-developer-dashboard" |
| 31 | +) |
| 32 | + |
| 33 | +do { |
| 34 | + let requestBody = OpenAIChatCompletionRequestBody( |
| 35 | + model: "gpt-4o-2024-08-06", |
| 36 | + messages: [ |
| 37 | + .user(content: .text("How cold is it today in SF?")) |
| 38 | + ], |
| 39 | + tools: MyFunctionTools().toOpenAITools() |
| 40 | + ) |
| 41 | + |
| 42 | + let response = try await openAIService.chatCompletionRequest(body: requestBody) |
| 43 | + if let toolCall = response.choices.first?.message.toolCalls?.first { |
| 44 | + let functionName = toolCall.function.name |
| 45 | + let arguments = toolCall.function.arguments ?? [:] |
| 46 | + print("ChatGPT wants us to call function \(functionName) with arguments: \(arguments)") |
| 47 | + } else { |
| 48 | + print("Could not get function arguments") |
| 49 | + } |
| 50 | + |
| 51 | +} catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) { |
| 52 | + print("Received \(statusCode) status code with response body: \(responseBody)") |
| 53 | +} catch { |
| 54 | + print("Could not make an OpenAI structured output tool call: \(error.localizedDescription)") |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Installation |
| 59 | + |
| 60 | +### Swift Package Manager |
| 61 | + |
| 62 | +``` |
| 63 | +let package = Package( |
| 64 | + name: "MyPackage", |
| 65 | + products: [...], |
| 66 | + targets: [ |
| 67 | + .target( |
| 68 | + "YouAppModule", |
| 69 | + dependencies: [ |
| 70 | + .product(name: "FunctionCalling-AIProxySwift", package: "FunctionCalling-AIProxySwift") |
| 71 | + ] |
| 72 | + ) |
| 73 | + ], |
| 74 | + dependencies: [ |
| 75 | + .package(url: "https://github.com/FunctionCalling/FunctionCalling-AIProxySwift", from: "0.1.0") |
| 76 | + ] |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +## Contributing |
| 81 | + |
| 82 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 83 | + |
| 84 | +Please make sure to update tests as appropriate. |
| 85 | + |
| 86 | +## License |
| 87 | + |
| 88 | +Apache v2 License |
0 commit comments