Views
No views yet
embed-multilingual-v3.0 model. See our blogpost Cohere Embed V3 for more details on this model.pip install -U cohere1# This snippet shows and example how to use the Cohere Embed V3 models for semantic search.
2# Make sure to have the Cohere SDK in at least v4.30 install: pip install -U cohere
3# Get your API key from: www.cohere.com
4import cohere
5import numpy as np
6
7cohere_key = "{YOUR_COHERE_API_KEY}" #Get your API key from www.cohere.com
8co = cohere.Client(cohere_key)
9
10docs = ["The capital of France is Paris",
11 "PyTorch is a machine learning framework based on the Torch library.",
12 "The average cat lifespan is between 13-17 years"]
13
14
15#Encode your documents with input type 'search_document'
16doc_emb = co.embed(docs, input_type="search_document", model="embed-multilingual-v3.0").embeddings
17doc_emb = np.asarray(doc_emb)
18
19
20#Encode your query with input type 'search_query'
21query = "What is Pytorch"
22query_emb = co.embed([query], input_type="search_query", model="embed-multilingual-v3.0").embeddings
23query_emb = np.asarray(query_emb)
24query_emb.shape
25
26#Compute the dot product between query embedding and document embedding
27scores = np.dot(query_emb, doc_emb.T)[0]
28
29#Find the highest scores
30max_idx = np.argsort(-scores)
31
32print(f"Query: {query}")
33for idx in max_idx:
34 print(f"Score: {scores[idx]:.2f}")
35 print(docs[idx])
36 print("--------")