cosineSimilarity
Cosine similarity between two vectors.
Pure arithmetic, no network call. Returns a value from -1 to 1, where 1 means the vectors point the same way.
Vectors of different lengths, or a zero vector, return 0 rather than throwing.
let score = cosineSimilarity(queryVector, documentVector)Signature
func cosineSimilarity(
_ a: [Double],
_ b: [Double]
) -> DoubleDefined in Sources/AI/Core/Embeddings.swift.
Parameters
| Parameter | Type | Default |
|---|---|---|
_ | [Double] | required |
_ | [Double] | required |
Returns
A Double between -1 and 1.