Skip to content

Commit 182fe84

Browse files
committed
Added Cohere example [skip ci]
1 parent 13450b0 commit 182fe84

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ And follow the instructions for your database library:
2727
Or check out some examples:
2828

2929
- [Embeddings](https://github.com/pgvector/pgvector-python/blob/master/examples/openai_embeddings.py) with OpenAI
30+
- [Binary embeddings](https://github.com/pgvector/pgvector-python/blob/master/examples/cohere_embeddings.py) with Cohere
3031
- [Sentence embeddings](https://github.com/pgvector/pgvector-python/blob/master/examples/sentence_embeddings.py) with SentenceTransformers
3132
- [Hybrid search](https://github.com/pgvector/pgvector-python/blob/master/examples/hybrid_search_rrf.py) with SentenceTransformers (Reciprocal Rank Fusion)
3233
- [Hybrid search](https://github.com/pgvector/pgvector-python/blob/master/examples/hybrid_search.py) with SentenceTransformers (cross-encoder)

examples/cohere_embeddings.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cohere
2+
import numpy as np
3+
from pgvector.psycopg import register_vector, Bit
4+
import psycopg
5+
6+
conn = psycopg.connect(dbname='pgvector_example', autocommit=True)
7+
8+
conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
9+
register_vector(conn)
10+
11+
conn.execute('DROP TABLE IF EXISTS documents')
12+
conn.execute('CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1024))')
13+
14+
15+
def fetch_embeddings(input, input_type):
16+
co = cohere.Client()
17+
response = co.embed(texts=input, model='embed-english-v3.0', input_type=input_type, embedding_types=['ubinary'])
18+
return [np.unpackbits(np.array(embedding, dtype=np.uint8)) for embedding in response.embeddings.ubinary]
19+
20+
21+
input = [
22+
'The dog is barking',
23+
'The cat is purring',
24+
'The bear is growling'
25+
]
26+
embeddings = fetch_embeddings(input, 'search_document')
27+
for content, embedding in zip(input, embeddings):
28+
conn.execute('INSERT INTO documents (content, embedding) VALUES (%s, %s)', (content, Bit(embedding)))
29+
30+
query = 'forest'
31+
query_embedding = fetch_embeddings([query], 'search_query')[0]
32+
result = conn.execute('SELECT content FROM documents ORDER BY embedding <~> %s LIMIT 5', (Bit(query_embedding),)).fetchall()
33+
for row in result:
34+
print(row[0])

examples/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cohere
12
datasets
23
gensim
34
imagehash

0 commit comments

Comments
 (0)