Skip to content

Commit

Permalink
Fix for empty OpenAI functions array (#44)
Browse files Browse the repository at this point in the history
# Fix for empty functions array

## ♻️ Current situation & Problem
If no function calls are present and we pass an empty array of functions
to the OpenAI library, the OpenAI API call randomly returns an error
(only sometimes!)


## ⚙️ Release Notes 
- Fix OpenAI error return if function calls are empty


## 📚 Documentation
--


## ✅ Testing
Properly tested on simulator and real device.


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
philippzagar authored Jan 26, 2024
1 parent 4e1e3e7 commit 94c1f3b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Sources/SpeziLLMOpenAI/LLMOpenAI+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ extension LLMOpenAI {
/// in an OpenAI `ChatQuery` representation used for querying the OpenAI API.
var openAIChatQuery: ChatQuery {
get async {
await .init(
let functions: [ChatFunctionDeclaration] = self.functions.values.compactMap { function in
let functionType = Swift.type(of: function)

return .init(
name: functionType.name,
description: functionType.description,
parameters: function.schema
)
}

return await .init(
model: self.parameters.modelType,
messages: self.openAIContext,
responseFormat: self.modelParameters.responseFormat,
functions: self.functions.values.compactMap { function in
let functionType = Swift.type(of: function)

return .init(
name: functionType.name,
description: functionType.description,
parameters: function.schema
)
},
functions: functions.isEmpty ? nil : functions,
temperature: self.modelParameters.temperature,
topP: self.modelParameters.topP,
n: self.modelParameters.completionsPerOutput,
Expand Down

0 comments on commit 94c1f3b

Please sign in to comment.