Skip to content

Commit

Permalink
embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
monofuel committed Jan 13, 2024
1 parent 90d6e48 commit 59ca37f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/llama_leap.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ type
details*: ModelDetails
ListResp* = ref object
models*: seq[OllamaModel]
EmbeddingReq* = ref object
model*: string
prompt*: string
options*: Option[ModelParameters] # bag of model parameters
EmbeddingResp* = ref object
embedding*: seq[float64]

proc renameHook*(v: var GenerateReq, fieldName: var string) =
## `template` is a special keyword in nim, so we need to rename it during serialization
Expand Down Expand Up @@ -177,3 +183,25 @@ proc pullModel*(api: OllamaAPI, name: string) =
let status = respJson["status"].getStr
if status != "success":
raise newException(CatchableError, &"ollama pull bad status: {resp.body}")

proc generateEmbeddings*(
api: OllamaAPI,
model: string,
prompt: string,
options: Option[ModelParameters] = none(ModelParameters)
): EmbeddingResp =
## Get the embeddings for a prompt
let url = api.baseUrl / "embeddings"
var req = EmbeddingReq(
model: model,
prompt: prompt,
options: options
)

var headers: curly.HttpHeaders
headers["Content-Type"] = "application/json"

let resp = api.curlPool.post(url, headers, toJson(req), api.curlTimeout)
if resp.code != 200:
raise newException(CatchableError, &"ollama embedding failed: {resp.code} {resp.body}")
result = fromJson(resp.body, EmbeddingResp)
4 changes: 4 additions & 0 deletions tests/test_llama_leap.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ suite "llama_leap":
)
let resp2 = ollama.generate(req2)
echo "2> " & resp2.response.strip()
suite "embeddings":
test "generate embeddings":
let resp = ollama.generateEmbeddings(TestModel, "How are you today?")
echo "> " & resp.embedding.join(",")

0 comments on commit 59ca37f

Please sign in to comment.