Vector Space Vector Space

Text to vector conversion

Convert text into high-dimensional vectors using OpenAI's models.
Essential foundation for semantic search with detailed analysis and visualization.

← Back to Home

Interactive Demo

Text that will be converted to a numerical vector representation
Warning: This will display a very long list of numbers

Code Example

// OpenAI Embeddings Integration
val openAI = OpenAI(apiKey = "your-openai-api-key")

// Single text embedding
val embedding = openAI.createEmbedding("Machine learning is powerful")
println("Dimensions: ${embedding.size}")
println("First 5 values: ${embedding.take(5)}")

// Batch embeddings for multiple texts
val texts = listOf(
    "Machine learning is a subset of AI",
    "Deep learning uses neural networks",
    "Natural language processing handles text"
)
val embeddings = openAI.createEmbeddings(texts)

// Calculate similarity between embeddings
fun cosineSimilarity(a: List<Double>, b: List<Double>): Double {
    val dotProduct = a.zip(b).sumOf { it.first * it.second }
    val magnitudeA = sqrt(a.sumOf { it * it })
    val magnitudeB = sqrt(b.sumOf { it * it })
    return dotProduct / (magnitudeA * magnitudeB)
}

openAI.close()