update
This commit is contained in:
+46
-49
@@ -5,60 +5,57 @@ export get_embedding_nats
|
|||||||
using GeneralUtils, msghandler
|
using GeneralUtils, msghandler
|
||||||
|
|
||||||
|
|
||||||
"""
|
""" Fetch embeddings for a list of texts from a NATS-based embedding service.
|
||||||
get_embedding_nats(nats_server_url::String, texts::Vector{String}, sendto_topic::String, fileserver_url::String)
|
|
||||||
|
|
||||||
Fetch embeddings for a list of texts from a NATS-based embedding service.
|
# Arguments
|
||||||
|
- `nats_conn_str::String`: NATS server connection URL
|
||||||
|
- `texts::Vector{String}`: List of text strings to generate embeddings for
|
||||||
|
- `sendto_topic::String`: NATS topic to send the embedding request to
|
||||||
|
- `fileserver_url::String`: URL of the file server for additional data
|
||||||
|
|
||||||
# Arguments
|
# Returns
|
||||||
- `nats_server_url::String`: NATS server connection URL
|
- `Vector` of embeddings, where each embedding is a vector of floats (Vector{Float32} or Vector{Float64})
|
||||||
- `texts::Vector{String}`: List of text strings to generate embeddings for
|
|
||||||
- `sendto_topic::String`: NATS topic to send the embedding request to
|
|
||||||
- `fileserver_url::String`: URL of the file server for additional data
|
|
||||||
|
|
||||||
# Returns
|
# Example
|
||||||
- `Vector` of embeddings, where each embedding is a vector of floats (Vector{Float32} or Vector{Float64})
|
```julia
|
||||||
|
nats_conn_str = "nats://username:password@yiem.cc:4222"
|
||||||
|
texts = ["Hello world", "Another text"]
|
||||||
|
sendto_topic = "embedding.service"
|
||||||
|
fileserver_url = "http://localhost:8080"
|
||||||
|
embeddings = get_embedding_nats(nats_server_url, texts, sendto_topic, fileserver_url)
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
function get_embedding_nats(nats_conn_str::String, texts::Vector{String}, sendto_topic::String,
|
||||||
|
fileserver_url::String)
|
||||||
|
nats_conn = NATS.connect(nats_conn_str)
|
||||||
|
return get_embedding_nats(nats_conn, texts, sendto_topic, fileserver_url)
|
||||||
|
end
|
||||||
|
|
||||||
# Example
|
function get_embedding_nats(nats_conn::NATS.Connection, texts::Vector{String}, sendto_topic::String,
|
||||||
```julia
|
fileserver_url::String)
|
||||||
nats_server_url = "nats://localhost:4222"
|
println("Generating embeddings for $(length(texts)) texts...")
|
||||||
texts = ["Hello world", "Another text"]
|
documents_dict = Dict("documents" => texts)
|
||||||
sendto_topic = "embedding.service"
|
payloads = [("documents", documents_dict, "dictionary")]
|
||||||
fileserver_url = "http://localhost:8080"
|
_, msg_envelope_json_str = msghandler.smartpack(
|
||||||
embeddings = get_embedding_nats(nats_server_url, texts, sendto_topic, fileserver_url)
|
sendto_topic,
|
||||||
```
|
payloads;
|
||||||
"""
|
msg_purpose="embedding",
|
||||||
function get_embedding_nats(nats_server_url::String, texts::Vector{String}, sendto_topic::String,
|
fileserver_url=fileserver_url)
|
||||||
fileserver_url::String)
|
|
||||||
nats_conn = NATS.connect(nats_server_url)
|
reply = NATS.request(nats_conn,
|
||||||
return get_embedding_nats(nats_conn, texts, sendto_topic, fileserver_url)
|
sendto_topic,
|
||||||
end
|
msg_envelope_json_str, timeout=300)
|
||||||
|
incoming_env_json_str = String(reply.payload)
|
||||||
function get_embedding_nats(nats_conn::NATS.Connection, texts::Vector{String}, sendto_topic::String,
|
incoming_env = msghandler.smartunpack(incoming_env_json_str)
|
||||||
fileserver_url::String)
|
embedding_response = incoming_env["payloads"][1][2]
|
||||||
println("Generating embeddings for $(length(texts)) texts...")
|
|
||||||
documents_dict = Dict("documents" => texts)
|
result = []
|
||||||
payloads = [("documents", documents_dict, "dictionary")]
|
for i in embedding_response["data"]
|
||||||
_, msg_envelope_json_str = msghandler.smartpack(
|
embedding_vector = i["embedding"]
|
||||||
sendto_topic,
|
push!(result, embedding_vector)
|
||||||
payloads;
|
|
||||||
msg_purpose="embedding",
|
|
||||||
fileserver_url=fileserver_url)
|
|
||||||
|
|
||||||
reply = NATS.request(nats_conn,
|
|
||||||
sendto_topic,
|
|
||||||
msg_envelope_json_str, timeout=300)
|
|
||||||
incoming_env_json_str = String(reply.payload)
|
|
||||||
incoming_env = msghandler.smartunpack(incoming_env_json_str)
|
|
||||||
embedding_response = incoming_env["payloads"][1][2]
|
|
||||||
|
|
||||||
result = []
|
|
||||||
for i in embedding_response["data"]
|
|
||||||
embedding_vector = i["embedding"]
|
|
||||||
push!(result, embedding_vector)
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user