Skip to content

Instantly share code, notes, and snippets.

@sebsto
Created October 25, 2023 16:27
Show Gist options
  • Save sebsto/9ef3b9bdaed7b6493b7921a0482fdf19 to your computer and use it in GitHub Desktop.
Save sebsto/9ef3b9bdaed7b6493b7921a0482fdf19 to your computer and use it in GitHub Desktop.
Example of code to invoke Llama 2 LLM on Amazon Bedrock in the Swift programming language
import Foundation
import ClientRuntime
// reduce the verbosity of the AWS SDK
SDKLoggingSystem.initialize(logLevel: .warning)
import AWSBedrock
import AWSBedrockRuntime
// create a Bedrock client and list available models for a provider
let provider = "meta"
print("====== Models available for \(provider)")
let client = try BedrockClient(region: "us-east-1")
let input = ListFoundationModelsInput(byProvider : provider)
let output = try await client.listFoundationModels(input: input)
print(output.modelSummaries!.map { "\($0.modelName!) : \($0.modelId!)" }.joined(separator: "\n"))
print("======")
// create a bedrock runtime client and invoke a model
let modelId = "meta.llama2-13b-chat-v1"
let runtime = try BedrockRuntimeClient(region: "us-east-1")
let payload = LlamaChatMessage(prompt: "What is the difference between a llama and an alpaca?")
let request = InvokeModelInput(body: try payload.encode(),
contentType: "application/json",
modelId: modelId)
let response = try await runtime.invokeModel(input: request)
let llamaResponse = try LlamaChatResponse(from: response!.body!)
print(llamaResponse)
print("======")
struct LlamaChatMessage: Encodable {
let prompt: String
let maxGenLen: Int = 512
let topP: Double = 0.9
let temperature: Double = 0.2
func encode() throws -> Data {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
return try encoder.encode(self)
}
}
struct LlamaChatResponse: Decodable, CustomStringConvertible {
let generation: String
let promptTokenCount: Int
let generationTokenCount : Int
init(from data: Data) throws {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
self = try decoder.decode(LlamaChatResponse.self, from: data)
}
var description: String {
return generation.trim()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment