Skip to content

Commit

Permalink
better context
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanborror committed Oct 10, 2024
1 parent 515c072 commit b9bbe18
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Sources/GenKit/Sessions/ChatSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public struct ChatSessionRequest {
public private(set) var history: [Message] = []
public private(set) var tools: [Tool] = []
public private(set) var tool: Tool? = nil
public private(set) var context: [String] = []
public private(set) var context: [String: String] = [:]
public private(set) var temperature: Float? = nil

public init(service: ChatService, model: Model, toolCallback: ToolCallback? = nil) {
Expand Down Expand Up @@ -198,7 +198,7 @@ public struct ChatSessionRequest {
}
}

public mutating func with(context: [String]) {
public mutating func with(context: [String: String]) {
self.context = context
}

Expand All @@ -211,11 +211,11 @@ public struct ChatSessionRequest {

// Apply user context
var systemContext = ""
if !context.isEmpty {
if let memories = context["MEMORIES"] {
systemContext = """
The following is context about the current user:
<user_context>
\(context.joined(separator: "\n"))
\(memories)
</user_context>
"""
}
Expand Down
59 changes: 59 additions & 0 deletions Sources/GenKit/Sessions/PipelineSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//import Foundation
//
//public class PipelineSession {
// public static let shared = PipelineSession()
//
// public func completion(_ request: Pipeline.Request, runLoopLimit: Int = 10) async throws -> Pipeline.Response {
// var pipelineResponse = Pipeline.Response(steps: [])
//
// for step in request.steps {
// let instructions = PromptTemplate(step.instructions, with: step.inputs)
//
// var req = ChatSessionRequest(service: step.service, model: step.model)
// req.with(history: [.user(content: instructions)])
//
// let resp = try await ChatSession.shared.completion(req)
// let stepCompletion = Pipeline.Response.Step(
// instructions: instructions,
// inputs: step.inputs,
// outputs: step.outputs,
// messages: resp.messages
// )
// pipelineResponse.steps.append(stepCompletion)
//
// // TODO: Extract the expected output variables
// }
//
// return pipelineResponse
// }
//}
//
//public struct Pipeline {
//
// public struct Request: Sendable {
// public var steps: [Step]
//
// public struct Step: Sendable {
// public var service: ChatService
// public var model: Model
// public var instructions: String
// public var inputs: [String: String]
// public var outputs: [String: String]
// }
// }
//
// public struct Response: Codable, Sendable {
// public var steps: [Step]
//
// public struct Step: Codable, Sendable {
// public var instructions: String
// public var inputs: [String: String]
// public var outputs: [String: String]
// public var messages: [Message]
// }
// }
//}
//
//enum PipelineSessionError: Error {
// case unknown
//}
8 changes: 4 additions & 4 deletions Sources/GenKit/Sessions/VisionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public struct VisionSessionRequest {

public private(set) var system: String? = nil
public private(set) var history: [Message] = []
public private(set) var context: [String] = []
public private(set) var context: [String: String] = [:]
public private(set) var temperature: Float? = nil

public init(service: VisionService, model: Model, toolCallback: ToolCallback? = nil) {
Expand All @@ -87,7 +87,7 @@ public struct VisionSessionRequest {
self.history = history
}

public mutating func with(context: [String]) {
public mutating func with(context: [String: String]) {
self.context = context
}

Expand All @@ -100,11 +100,11 @@ public struct VisionSessionRequest {

// Apply user context
var systemContext = ""
if !context.isEmpty {
if let memories = context["MEMORIES"] {
systemContext = """
The following is context about the current user:
<user_context>
\(context.joined(separator: "\n"))
\(memories)
</user_context>
"""
}
Expand Down

0 comments on commit b9bbe18

Please sign in to comment.