Skip to content

Commit

Permalink
snake_case > camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
James J Kalafus committed Feb 15, 2024
1 parent 54a4427 commit ba5cd2b
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 124 deletions.
6 changes: 3 additions & 3 deletions Demo/DemoChat/Sources/ChatStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ public final class ChatStore: ObservableObject {
for choice in partialChatResult.choices {
let existingMessages = conversations[conversationIndex].messages
// Function calls are also streamed, so we need to accumulate.
choice.delta.tool_calls?.forEach { toolCallDelta in
choice.delta.toolCalls?.forEach { toolCallDelta in
if let functionCallDelta = toolCallDelta.function {
if let nameDelta = functionCallDelta.name {
functionCalls.append((nameDelta, functionCallDelta.arguments))
}
}
}
var messageText = choice.delta.content ?? ""
if let finishReason = choice.finish_reason,
finishReason == .tool_calls
if let finishReason = choice.finishReason,
finishReason == .toolCalls
{
functionCalls.forEach { (name: String, argument: String?) in
messageText += "Function call: name=\(name) arguments=\(argument ?? "")\n"
Expand Down
140 changes: 83 additions & 57 deletions Sources/OpenAI/Public/Models/ChatQuery.swift

Large diffs are not rendered by default.

50 changes: 40 additions & 10 deletions Sources/OpenAI/Public/Models/ChatResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct ChatResult: Codable, Equatable {
/// A chat completion message generated by the model.
public let message: Self.ChatCompletionMessage
/// The reason the model stopped generating tokens. This will be stop if the model hit a natural stop point or a provided stop sequence, length if the maximum number of tokens specified in the request was reached, content_filter if content was omitted due to a flag from our content filters, tool_calls if the model called a tool, or function_call (deprecated) if the model called a function.
public let finish_reason: String?
public let finishReason: String?

public struct ChoiceLogprobs: Codable, Equatable {

Expand All @@ -38,7 +38,7 @@ public struct ChatResult: Codable, Equatable {
public let logprob: Double
/// List of the most likely tokens and their log probability, at this token position.
/// In rare cases, there may be fewer than the number of requested `top_logprobs` returned.
public let top_logprobs: [TopLogprob]
public let topLogprobs: [TopLogprob]

public struct TopLogprob: Codable, Equatable {

Expand All @@ -50,26 +50,46 @@ public struct ChatResult: Codable, Equatable {
/// The log probability of this token.
public let logprob: Double
}

public enum CodingKeys: String, CodingKey {
case token
case bytes
case logprob
case topLogprobs = "top_logprobs"
}
}
}


public enum CodingKeys: String, CodingKey {
case index
case logprobs
case message
case finishReason = "finish_reason"
}

public enum FinishReason: String, Codable, Equatable {
case stop
case length
case tool_calls
case content_filter
case function_call
case toolCalls = "tool_calls"
case contentFilter = "content_filter"
case functionCall = "function_call"
}
}

public struct CompletionUsage: Codable, Equatable {

/// Number of tokens in the generated completion.
public let completion_tokens: Int
public let completionTokens: Int
/// Number of tokens in the prompt.
public let prompt_tokens: Int
public let promptTokens: Int
/// Total number of tokens used in the request (prompt + completion).
public let total_tokens: Int
public let totalTokens: Int

enum CodingKeys: String, CodingKey {
case completionTokens = "completion_tokens"
case promptTokens = "prompt_tokens"
case totalTokens = "total_tokens"
}
}

/// A unique identifier for the chat completion.
Expand All @@ -86,7 +106,17 @@ public struct ChatResult: Codable, Equatable {
public let usage: Self.CompletionUsage?
/// This fingerprint represents the backend configuration that the model runs with.
/// Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism.
public let system_fingerprint: String?
public let systemFingerprint: String?

public enum CodingKeys: String, CodingKey {
case id
case object
case created
case model
case choices
case usage
case systemFingerprint = "system_fingerprint"
}
}

extension ChatQuery.ChatCompletionMessageParam {
Expand Down
38 changes: 33 additions & 5 deletions Sources/OpenAI/Public/Models/ChatStreamResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct ChatStreamResult: Codable, Equatable {
public let content: String?
/// The role of the author of this message.
public let role: Self.Role?
public let tool_calls: [Self.ChoiceDeltaToolCall]?
public let toolCalls: [Self.ChoiceDeltaToolCall]?

public struct ChoiceDeltaToolCall: Codable, Equatable {

Expand Down Expand Up @@ -58,6 +58,12 @@ public struct ChatStreamResult: Codable, Equatable {
}
}
}

public enum CodingKeys: String, CodingKey {
case content
case role
case toolCalls = "tool_calls"
}
}

/// The index of the choice in the list of choices.
Expand All @@ -66,7 +72,7 @@ public struct ChatStreamResult: Codable, Equatable {
public let delta: Self.ChoiceDelta
/// The reason the model stopped generating tokens.
/// This will be `stop` if the model hit a natural stop point or a provided stop sequence, `length` if the maximum number of tokens specified in the request was reached, `content_filter` if content was omitted due to a flag from our content filters, `tool_calls` if the model called a tool, or `function_call` (deprecated) if the model called a function.
public let finish_reason: FinishReason?
public let finishReason: FinishReason?
/// Log probability information for the choice.
public let logprobs: Self.ChoiceLogprobs?

Expand All @@ -82,7 +88,7 @@ public struct ChatStreamResult: Codable, Equatable {
/// The log probability of this token.
public let logprob: Double
/// List of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested top_logprobs returned.
public let top_logprobs: [Self.TopLogprob]?
public let topLogprobs: [Self.TopLogprob]?

public struct TopLogprob: Codable, Equatable {
/// The token.
Expand All @@ -92,8 +98,22 @@ public struct ChatStreamResult: Codable, Equatable {
/// The log probability of this token.
public let logprob: Double
}

public enum CodingKeys: String, CodingKey {
case token
case bytes
case logprob
case topLogprobs = "top_logprobs"
}
}
}

public enum CodingKeys: String, CodingKey {
case index
case delta
case finishReason = "finish_reason"
case logprobs
}
}

/// A unique identifier for the chat completion. Each chunk has the same ID.
Expand All @@ -109,6 +129,14 @@ public struct ChatStreamResult: Codable, Equatable {
/// Can be more than one if `n` is greater than 1.
public let choices: [Choice]
/// This fingerprint represents the backend configuration that the model runs with. Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
public let system_fingerprint: String?

public let systemFingerprint: String?

public enum CodingKeys: String, CodingKey {
case id
case object
case created
case model
case choices
case systemFingerprint = "system_fingerprint"
}
}
13 changes: 10 additions & 3 deletions Sources/OpenAI/Public/Models/EmbeddingsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public struct EmbeddingsQuery: Codable {
public let model: Model
/// The format to return the embeddings in. Can be either float or base64.
/// https://pypi.org/project/pybase64/
public let encoding_format: Self.EncodingFormat?
public let encodingFormat: Self.EncodingFormat?
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids
public let user: String?

public init(
input: Self.Input,
model: Model,
encoding_format: Self.EncodingFormat? = nil,
encodingFormat: Self.EncodingFormat? = nil,
user: String? = nil
) {
self.input = input
self.model = model
self.encoding_format = encoding_format
self.encodingFormat = encodingFormat
self.user = user
}

Expand Down Expand Up @@ -75,4 +75,11 @@ public struct EmbeddingsQuery: Codable {
case float
case base64
}

public enum CodingKeys: String, CodingKey {
case input
case model
case encodingFormat = "encoding_format"
case user
}
}
19 changes: 15 additions & 4 deletions Sources/OpenAI/Public/Models/ImageEditsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct ImageEditsQuery: Codable {
public let n: Int?
/// The format in which the generated images are returned. Must be one of url or b64_json.
/// Defaults to url
public let response_format: Self.ResponseFormat?
public let responseFormat: Self.ResponseFormat?
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
public let size: Size?
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
Expand All @@ -37,7 +37,7 @@ public struct ImageEditsQuery: Codable {
mask: Data? = nil,
model: Model? = nil,
n: Int? = nil,
response_format: Self.ResponseFormat? = nil,
responseFormat: Self.ResponseFormat? = nil,
size: Self.Size? = nil,
user: String? = nil
) {
Expand All @@ -46,10 +46,21 @@ public struct ImageEditsQuery: Codable {
self.prompt = prompt
self.model = model
self.n = n
self.response_format = response_format
self.responseFormat = responseFormat
self.size = size
self.user = user
}

public enum CodingKeys: String, CodingKey {
case image
case mask
case prompt
case model
case n
case responseFormat = "response_format"
case size
case user
}
}

extension ImageEditsQuery: MultipartFormDataBodyEncodable {
Expand All @@ -58,7 +69,7 @@ extension ImageEditsQuery: MultipartFormDataBodyEncodable {
.file(paramName: "image", fileName: "image.png", fileData: image, contentType: "image/png"),
.file(paramName: "mask", fileName: "mask.png", fileData: mask, contentType: "image/png"),
.string(paramName: "model", value: model),
.string(paramName: "response_format", value: response_format),
.string(paramName: "response_format", value: responseFormat),
.string(paramName: "user", value: user),
.string(paramName: "prompt", value: prompt),
.string(paramName: "n", value: n),
Expand Down
17 changes: 13 additions & 4 deletions Sources/OpenAI/Public/Models/ImageVariationsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct ImageVariationsQuery: Codable {
public let n: Int?
/// The format in which the generated images are returned. Must be one of url or b64_json.
/// Defaults to url
public let response_format: Self.ResponseFormat?
public let responseFormat: Self.ResponseFormat?
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// Defaults to 1024x1024
public let size: String?
Expand All @@ -32,25 +32,34 @@ public struct ImageVariationsQuery: Codable {
image: Data,
model: Model? = nil,
n: Int? = nil,
response_format: Self.ResponseFormat? = nil,
responseFormat: Self.ResponseFormat? = nil,
size: String? = nil,
user: String? = nil
) {
self.image = image
self.model = model
self.n = n
self.response_format = response_format
self.responseFormat = responseFormat
self.size = size
self.user = user
}

public enum CodingKeys: String, CodingKey {
case image
case model
case n
case responseFormat = "response_format"
case size
case user
}
}

extension ImageVariationsQuery: MultipartFormDataBodyEncodable {
func encode(boundary: String) -> Data {
let bodyBuilder = MultipartFormDataBodyBuilder(boundary: boundary, entries: [
.file(paramName: "image", fileName: "image.png", fileData: image, contentType: "image/png"),
.string(paramName: "model", value: model),
.string(paramName: "response_format", value: response_format),
.string(paramName: "responseFormat", value: responseFormat),
.string(paramName: "user", value: user),
.string(paramName: "n", value: n),
.string(paramName: "size", value: size)
Expand Down
10 changes: 8 additions & 2 deletions Sources/OpenAI/Public/Models/ImagesResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ public struct ImagesResult: Codable, Equatable {
public struct Image: Codable, Equatable {

/// The base64-encoded JSON of the generated image, if response_format is b64_json
public let b64_json: String?
public let b64Json: String?
/// The prompt that was used to generate the image, if there was any revision to the prompt.
public let revised_prompt: String?
public let revisedPrompt: String?
/// The URL of the generated image, if response_format is url (default).
public let url: String?

public enum CodingKeys: String, CodingKey {
case b64Json = "b64_json"
case revisedPrompt = "revised_prompt"
case url
}
}
}
9 changes: 8 additions & 1 deletion Sources/OpenAI/Public/Models/Models/ModelResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ public struct ModelResult: Codable, Equatable {
/// The object type, which is always "model".
public let object: String
/// The organization that owns the model.
public let owned_by: String
public let ownedBy: String

public enum CodingKeys: String, CodingKey {
case id
case created
case object
case ownedBy = "owned_by"
}
}
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public protocol OpenAIProtocol {
Example:
```
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, response_format: .mp3, speed: 1.0)
let query = AudioSpeechQuery(model: .tts_1, input: "Hello, world!", voice: .alloy, responseFormat: .mp3, speed: 1.0)
openAI.audioCreateSpeech(query: query) { result in
// Handle response here
}
Expand Down
Loading

0 comments on commit ba5cd2b

Please sign in to comment.