-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/PreternaturalAI/AI into ENG…
…-1490
- Loading branch information
Showing
25 changed files
with
404 additions
and
331 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
66 changes: 66 additions & 0 deletions
66
...LargeLanguageModels/Intramodular/LLMs/AbstractLLM.ChatCompletionDecodableResultType.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,66 @@ | ||
// | ||
// Copyright (c) Vatsal Manot | ||
// | ||
|
||
import Swallow | ||
import SwiftUIX | ||
|
||
extension AbstractLLM { | ||
// Specify the expected result type for strongly-typed results. For example, if you're executing a chat completion, the expected result will be a `.string` type. | ||
public struct ChatCompletionDecodableResultType<T: AbstractLLM.ChatCompletionDecodable> { | ||
fileprivate init() { | ||
|
||
} | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == String { | ||
public static var string: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == SwiftUIX._AnyImage { | ||
public static var image: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == AbstractLLM.ChatMessage { | ||
public static var chatMessage: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == AbstractLLM.ChatFunctionCall { | ||
public static var functionCall: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == Array<AbstractLLM.ChatFunctionCall> { | ||
public static var functionCalls: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == AbstractLLM.ResultOfFunctionCall { | ||
public static var functionInvocation: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType where T == Array<AbstractLLM.ResultOfFunctionCall> { | ||
public static var functionInvocations: Self { | ||
.init() | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatCompletionDecodableResultType { | ||
public static func either<LHS: AbstractLLM.ChatCompletionDecodable, RHS: AbstractLLM.ChatCompletionDecodable>( | ||
_ lhs: AbstractLLM.ChatCompletionDecodableResultType<LHS>, | ||
or rhs: AbstractLLM.ChatCompletionDecodableResultType<RHS> | ||
) -> Self where Self == AbstractLLM.ChatCompletionDecodableResultType<Either<LHS, RHS>> { | ||
.init() | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
...ces/LargeLanguageModels/Intramodular/LLMs/Chat/AbstractLLM.ChatMessage-Initializers.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,161 @@ | ||
// | ||
// Copyright (c) Vatsal Manot | ||
// | ||
|
||
import CorePersistence | ||
import Swallow | ||
import SwiftUIX | ||
|
||
extension AbstractLLM.ChatMessage { | ||
public init( | ||
id: AnyPersistentIdentifier? = nil, | ||
role: AbstractLLM.ChatRole, | ||
content: String | ||
) { | ||
self.init( | ||
id: id, | ||
role: role, | ||
content: PromptLiteral(stringLiteral: content) | ||
) | ||
} | ||
|
||
public init( | ||
id: UUID, | ||
role: AbstractLLM.ChatRole, | ||
content: String | ||
) { | ||
self.init( | ||
id: AnyPersistentIdentifier(erasing: id), | ||
role: role, | ||
content: content | ||
) | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatMessage { | ||
public static func system( | ||
_ content: PromptLiteral | ||
) -> Self { | ||
Self(role: .system, content: content) | ||
} | ||
|
||
public static func system( | ||
_ content: () throws -> PromptLiteral | ||
) rethrows -> Self { | ||
Self(role: .system, content: try content()) | ||
} | ||
|
||
public static func system( | ||
_ content: String | ||
) -> Self { | ||
Self(role: .system, content: content) | ||
} | ||
|
||
public static func system( | ||
_ content: () throws -> String | ||
) rethrows -> Self { | ||
Self(role: .system, content: try content()) | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatMessage { | ||
public static func assistant( | ||
_ content: PromptLiteral | ||
) -> Self { | ||
Self(role: .assistant, content: content) | ||
} | ||
|
||
public static func assistant( | ||
_ content: () throws -> PromptLiteral | ||
) rethrows -> Self { | ||
Self(role: .assistant, content: try content()) | ||
} | ||
|
||
public static func assistant( | ||
_ content: String | ||
) -> Self { | ||
Self(role: .assistant, content: content) | ||
} | ||
|
||
public static func assistant( | ||
_ content: () throws -> String | ||
) rethrows -> Self { | ||
Self(role: .assistant, content: try content()) | ||
} | ||
|
||
/// A function call. | ||
public static func functionCall( | ||
_ functionCall: AbstractLLM.ChatFunctionCall | ||
) -> Self { | ||
Self(role: .assistant, content: try! PromptLiteral(functionCall: functionCall)) | ||
} | ||
|
||
/// The function call of a given function, with its arguments expressed as JSON. | ||
public static func functionCall( | ||
of function: AbstractLLM.ChatFunctionDefinition, | ||
arguments: JSON | ||
) -> Self { | ||
Self( | ||
role: .assistant, | ||
content: try! PromptLiteral( | ||
functionCall: AbstractLLM.ChatFunctionCall( | ||
functionID: nil, // FIXME: !!! | ||
name: function.name, | ||
arguments: .init(unencoded: arguments.prettyPrintedDescription), | ||
context: .init() | ||
) | ||
) | ||
) | ||
} | ||
|
||
/// A function invocation is a function call + the result. | ||
/// | ||
/// Conceptually, this represents the function call as the LLM would invoke it _including_ the function's result. | ||
/// | ||
/// You can construct it manually as part of few-shot prompting to guide the LLM on how to call your function. | ||
/// | ||
/// This is **not** the same thing as just a 'function call'. A function call is **only** the function name + the parameters that the LLM generates to invoke it, _without_ the actual result of the function. | ||
public static func functionInvocation( | ||
_ functionInvocation: AbstractLLM.ResultOfFunctionCall | ||
) -> Self { | ||
Self( | ||
role: .other(.function), | ||
content: try! PromptLiteral( | ||
functionInvocation: functionInvocation, | ||
role: .chat(.other(.function)) | ||
) | ||
) | ||
} | ||
} | ||
|
||
extension AbstractLLM.ChatMessage { | ||
public static func user( | ||
_ content: PromptLiteral | ||
) -> Self { | ||
Self(role: .user, content: content) | ||
} | ||
|
||
public static func user( | ||
_ content: AppKitOrUIKitImage | ||
) -> Self { | ||
Self(role: .user, content: try! PromptLiteral(image: content)) | ||
} | ||
|
||
public static func user( | ||
_ content: () throws -> PromptLiteral | ||
) rethrows -> Self { | ||
Self(role: .user, content: try content()) | ||
} | ||
|
||
public static func user( | ||
_ content: String | ||
) -> Self { | ||
Self(role: .user, content: content) | ||
} | ||
|
||
public static func user( | ||
_ content: () throws -> String | ||
) rethrows -> Self { | ||
Self(role: .user, content: try content()) | ||
} | ||
} |
Oops, something went wrong.