Comments (12)
I created a pull request that keeps the id and Identifiable conformance in place, which I think is preferable when using it with SwiftUI, but skips the id when encoding. Hashable and equatable conformance can then be done with a similar extension as above if needed.
extension ChatMessage: Hashable {
public static func == (lhs: ChatMessage, rhs: ChatMessage) -> Bool {
lhs.id == rhs.id &&
lhs.content == rhs.content
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
@Mmanion maybe you'd prefer this over a rollback 🙃
from openaiswift.
I got this error, too.
It's may produced by: #94
from openaiswift.
Confirmed this fixes the error... I dont know if it fixes the Swift Identifciable issue as I dont use SwiftUI
from openaiswift.
@marcoboerner Yup, thats a much better fix then mine. I ran it in SwiftUI and it works, just closed my PR. Great job!
from openaiswift.
The only thing that you need to be aware of when streaming is, currently with every updated ChatMessage a new UUID is generated. It's fine if you're just replacing the last ChatMessage of an array with the updated ChatMessage, but if you try to work with a set
or try to use the id
to find the ChatMessage that is being updated, that won't work. But I think that's rather an implementation detail in the stream logic.
from openaiswift.
@tingspain are you using the last pre-release version? The PRs to fix this issue have not been released. You may want to switch to an earlier version or use the branch of the PR with the last main
commit.
from openaiswift.
add this extension instead:
import OpenAISwift
import Foundation
extension ChatMessage: Identifiable, Equatable {
public var id: UUID {
return UUID()
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public static func == (lhs: ChatMessage, rhs: ChatMessage) -> Bool {
return lhs.id == rhs.id
}
}
from openaiswift.
I'm using SwiftUI, and it doesn't seems to solve the problem.
Created a PR to remove it. #106
Definitely like the idea of ChatMessages being Identifiable. If I have the time, will see how to implement it while preventing this bug.
from openaiswift.
All your PR does is rollback to the previous version. Is the extension provided by rom4in not working ? As from the documentation it appears that it should.
In Swift, extension properties do not have to comply with Codable directly. Codable is a protocol used for encoding and decoding Swift types to and from external representations like JSON or Plist.
When you conform a type to Codable, you are essentially telling Swift how to convert an instance of that type to and from a JSON representation. The properties that are encoded and decoded must themselves be Codable.
If you extend a type with additional properties through an extension, those extension properties are not automatically considered when encoding or decoding using Codable. Only the properties declared in the original type or any extensions that directly implement Codable will be considered.
from openaiswift.
No, the extension by rom4in did not work for SwiftUI.
Yes, it is a rollback.
Not sure which part of the documentation you're referring to but definitely give this a try in SwiftUI (with the solutions mentioned above) and you'll see.
from openaiswift.
It is not working for me. I dont know what i am doing wrong. I keep getting the following error:
chatError(error: OpenAISwift.ChatError.Payload(message: "Additional properties are not allowed (\'id\' was unexpected) - \'messages.0\'", type: "invalid_request_error", param: nil, code: nil))
Could anyone point me on the right way to do it?
This is my code:
extension ChatMessage: Identifiable, Equatable, Hashable {
public var id: UUID {
return UUID()
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public static func == (lhs: ChatMessage, rhs: ChatMessage) -> Bool {
lhs.id == rhs.id &&
lhs.content == rhs.content
}
}
class OpenAIChat : ObservableObject{
let openAI = OpenAISwift(config: .makeDefaultOpenAI(apiKey: "sk-"))
var message: String = ""
func send(question: String = "Hello, how are you?") async {
print("Before send")
print("QUESTION: \(question)")
do {
let chat: [ChatMessage] = [
ChatMessage(role: .system, content: "You are a helpful assistant."),
ChatMessage(role: .user, content: "Who won the world series in 2020?")
]
let result = try await openAI.sendChat(
with: chat,
model: .chat(.chatgpt),
choices: 1,
maxTokens: 256
)
print("RESULT: ")
print( result )
} catch {
print(error)
}
print("After send")
}
}
from openaiswift.
@marcoboerner, ah. Let me switch to the PR and test it. Thank you very much!!!
from openaiswift.
Related Issues (20)
- .gpt4 is not working, returning empty string HOT 1
- your code is broken. HOT 2
- authToken doesn't work in version 1.4 HOT 6
- "The operation couldn’t be completed. (OpenAISwift.OpenAIError error 2.)" HOT 2
- Always returning nil HOT 3
- OpenAIEndpointModelType that replaces the OpenAIModelType so only compatible models are available for each method
- Trying to take a sendCompletion request with gpt4 model HOT 1
- Cannot use instance member 'key' within property initializer; property initializers run before 'self' is available
- no response from openAI webAPI? HOT 3
- OpenAISwift 1.4.0 multiple errors with new Xcode 15 release HOT 1
- theres a typo for URLRequest it say inout instead of "input" HOT 1
- Stay away from this package HOT 12
- OpenAISwift - New Fork with merged Pull Requests
- Latest Models & Updates HOT 1
- 2023 Dev Conf Updates
- Is gpt-4-vision-preview model possible? HOT 1
- Not getting any responses from API HOT 3
- Updated Jan 2024 Models and Endpoints
- Simple request
- Please add support for setting base_url HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openaiswift.