Skip to content

Instantly share code, notes, and snippets.

@zilto
Last active July 5, 2023 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zilto/2ebb4d6c8375d19969473beb3ec27230 to your computer and use it in GitHub Desktop.
Save zilto/2ebb4d6c8375d19969473beb3ec27230 to your computer and use it in GitHub Desktop.
# functions from the same file; embedding_module.py
@config.when(embedding_service="openai")
def embeddings__openai(
embedding_provider: ModuleType,
text_contents: list[str],
model_name: str = "text-embedding-ada-002",
) -> list[np.ndarray]:
"""Convert text to vector representations (embeddings) using OpenAI Embeddings API
reference: https://github.com/openai/openai-cookbook/blob/main/examples/Get_embeddings.ipynb
"""
response = embedding_provider.Embedding.create(input=text_contents, engine=model_name)
return [np.asarray(obj["embedding"]) for obj in response["data"]]
@config.when(embedding_service="cohere")
def embeddings__cohere(
embedding_provider: cohere.Client,
text_contents: list[str],
model_name: str = "embed-english-light-v2.0",
) -> list[np.ndarray]:
"""Convert text to vector representations (embeddings) using Cohere Embed API
reference: https://docs.cohere.com/reference/embed
"""
response = embedding_provider.embed(
texts=text_contents,
model=model_name,
truncate="END",
)
return [np.asarray(embedding) for embedding in response.embeddings]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment