Skip to content

Commit

Permalink
Adds support for the embeddings endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: ActuallyTaylor <[email protected]>
  • Loading branch information
ActuallyTaylor committed Nov 20, 2023
1 parent 5a0001f commit b2820b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 56 deletions.
2 changes: 2 additions & 0 deletions Sources/SLlama/Extensions/URLRequest+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extension URLRequest {

httpMethod = request.method.name
httpBody = try request.method.httpBody

addValue("application/json", forHTTPHeaderField: "Content-Type")
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/SLlama/LlamaRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public enum LlamaRequests {
public static func completion(_ parameters: CompletionSettings) -> Request<CompletionResult> {
return Request(path: "/completion", method: .post(.body(model: parameters)))
}

public static func embedding(_ parameters: EmbeddingSettings) -> Request<EmbeddingResults> {
return Request(path: "/embedding", method: .post(.body(model: parameters)))
}
}
12 changes: 12 additions & 0 deletions Sources/SLlama/Models/EmbeddingResults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Taylor Lineman on 11/19/23.
//

import Foundation

public struct EmbeddingResults: Codable {
public let embedding: [Double]
}
13 changes: 13 additions & 0 deletions Sources/SLlama/Models/EmbeddingSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
//
//
// Created by Taylor Lineman on 11/19/23.
//

import Foundation

public struct EmbeddingSettings: Codable {
/// Text to process
let content: String
}
51 changes: 0 additions & 51 deletions Tests/SLlamaTests/Return Data/CompletionResponse1.json

This file was deleted.

19 changes: 14 additions & 5 deletions Tests/SLlamaTests/SLlamaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ final class SLlamaTests: XCTestCase {
"""

func testEmbeddings() async throws {
let client = Client(baseURLString: "http://127.0.0.1:8080")

let settings: EmbeddingSettings = .init(content: "This is some contents to embed")

let request = LlamaRequests.embedding(settings)

_ = try await client.run(request)
}

func testCompletionEndpoint() async throws {
let client = Client(baseURLString: "http://127.0.0.1:8080")

Expand All @@ -22,15 +32,14 @@ final class SLlamaTests: XCTestCase {

let request = LlamaRequests.completion(settings)

let response = try await client.run(request)

print(response.content)
_ = try await client.run(request)
}

func testCompletionStreaming() async throws {
let client = Client(baseURLString: "http://127.0.0.1:8080")
let client = Client(baseURLString: "http://127.0.0.1:24445")

let preparedPrompt = PromptProcessor.prepareTemplate(template: template, systemPrompt: "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.", userPrompt: "Hello Assistant")

let settings: CompletionSettings = .init(prompt: preparedPrompt, temperature: 0.7, n_predict: 256, stream: true)

let request = LlamaRequests.completion(settings)
Expand All @@ -50,7 +59,7 @@ final class SLlamaTests: XCTestCase {
extension SLlamaTests: ClientStreamDelegate {
func didReceiveModel<Model>(model: Model) where Model : Codable {
guard let model = model as? CompletionResult else { return }
print(model.content)
print(model.content, terminator: "")
}

func didFinish(error: Error?) {
Expand Down

0 comments on commit b2820b4

Please sign in to comment.