DokumentationEmbeddings
Entwickeln
Embeddings
Ein Embedding übersetzt einen Text in eine Liste von Zahlen. Zwei Texte mit ähnlicher Bedeutung ergeben ähnliche Listen.
Vektoren erstellen
Senden Sie einen Text oder eine Liste von Texten. Jeder kommt mit seinem Vektor zurück, in derselben Reihenfolge.
passages = [
"Either party may terminate with three months' notice.",
"Invoices are payable within thirty days.",
]
result = client.embeddings.create(
model="learnya-embed", input=passages
)
vectors = [item.embedding for item in result.data]
print(len(vectors), "vectors of", len(vectors[0]), "numbers")const passages = [
"Either party may terminate with three months' notice.",
"Invoices are payable within thirty days.",
]
const result = await client.embeddings.create({
model: "learnya-embed",
input: passages,
})
const vectors = result.data.map((item) => item.embedding)
console.log(
vectors.length,
"vectors of",
vectors[0].length,
"numbers",
)const passages: string[] = [
"Either party may terminate with three months' notice.",
"Invoices are payable within thirty days.",
]
const result = await client.embeddings.create({
model: "learnya-embed",
input: passages,
})
const vectors: number[][] = result.data.map(
(item) => item.embedding,
)
console.log(
vectors.length,
"vectors of",
vectors[0].length,
"numbers",
)curl https://api.learnya.ai/v1/embeddings \
-H "Authorization: Bearer $LEARNYA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "learnya-embed",
"input": ["Invoices are payable within thirty days."]
}'Antwort
{
"object": "list",
"model": "learnya-embed",
"data": [
{ "object": "embedding", "index": 0, "embedding": [0.0123, -0.0481, 0.0277, -0.0019] }
],
"usage": { "prompt_tokens": 9, "total_tokens": 9 }
}Nach Bedeutung suchen
Vergleichen Sie den Vektor einer Frage mit denen Ihrer Textpassagen. Die nächstgelegene behandelt das Thema, auch mit anderen Worten.
import math
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
return dot / (
math.sqrt(sum(x * x for x in a))
* math.sqrt(sum(y * y for y in b))
)
question = client.embeddings.create(
model="learnya-embed",
input=["Can we end the contract early?"],
)
q = question.data[0].embedding
best = max(
range(len(passages)), key=lambda i: cosine(q, vectors[i])
)
print(passages[best])const cosine = (a, b) => {
let dot = 0,
na = 0,
nb = 0
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i]
na += a[i] * a[i]
nb += b[i] * b[i]
}
return dot / (Math.sqrt(na) * Math.sqrt(nb))
}
const question = await client.embeddings.create({
model: "learnya-embed",
input: ["Can we end the contract early?"],
})
const q = question.data[0].embedding
const scores = vectors.map((v) => cosine(q, v))
console.log(passages[scores.indexOf(Math.max(...scores))])const cosine = (a: number[], b: number[]): number => {
let dot = 0,
na = 0,
nb = 0
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i]
na += a[i] * a[i]
nb += b[i] * b[i]
}
return dot / (Math.sqrt(na) * Math.sqrt(nb))
}
const question = await client.embeddings.create({
model: "learnya-embed",
input: ["Can we end the contract early?"],
})
const q: number[] = question.data[0].embedding
const scores = vectors.map((v) => cosine(q, v))
console.log(passages[scores.indexOf(Math.max(...scores))])