近期微软宣布与 Cohere 合作,使客户能够通过 Azure AI Studio 模型目录轻松利用Cohere 模型,包括 Cohere最新的 LLM - Command R+。今天,我们很高兴地宣布,您可以使用Azure AI搜索存储和搜索Cohere最新的Embed-V3-Multilingual和 Embed V3-English int8嵌入。此功能可显着降低内存成本,同时通常保持较高的搜索质量,使其成为对大型数据集进行语义搜索的理想解决方案,为生成式 AI 应用程序提供支持。

“借助Azure AI 搜索中提供的int8 Cohere 嵌入,Cohere和 Azure用户现在都可以使用内存优化的嵌入模型和最先进的检索系统来运行高级 RAG 。” - Cohere机器总监Nils Reimers学习,

目前建议企业通过微软官方合作伙伴获取服务,可以合规、稳定地提供企业用户使用ChatGPT的可能,满足国内发票需求,同时也能解决连接不稳定/响应速度慢/并发配额低等问题。

通过 int8 嵌入,客户可以节省 4 倍的内存,并将搜索速度提高约 30%,同时保持 99.99% 的搜索质量。

以下是有关如何将 Cohere Embed V3 int8 嵌入与 Azure AI 搜索结合使用的分步指南:

安装所需的库

安装必要的库,包括Azure 搜索 Python SDKCohere Python SDK。请注意,请确保您使用我们最新的 2024-03-01-Preview API 版本。

pip install --pre azure-search-documents
pip install azure-identity

设置 Cohere 和 Azure AI 搜索凭据

设置 Cohere 和 Azure AI 搜索的凭据。设置 Cohere 和 Azure AI 搜索的凭据。您可以在Cohere 仪表板和Azure AI 搜索服务中的Azure 门户的 “密钥”边栏选项卡中找到这些内容。

cohere_api_key = os.getenv("COHERE_API_KEY") 
co = cohere.Client(cohere_api_key) 
search_service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT") 
search_service_api_key = os.getenv("AZURE_SEARCH_ADMIN_KEY") 
index_name = "cohere-embed-v3-index" 
credential = AzureKeyCredential(search_service_api_key)

生成嵌入函数

使用 Cohere Embed API 为文档列表生成 int8 嵌入。

def generate_embeddings(texts, input_type="search_document"): 
    model = "embed-english-v3.0" 
    response = co.embed( 
        texts=texts, 
        model=model, 
        input_type=input_type, 
        embedding_types=["int8"], 
    ) 
    return [embedding for embedding in response.embeddings.int8]

创建 Azure AI 搜索索引

创建或更新 Azure AI 搜索索引以包含用于存储文档嵌入的矢量字段。

def create_or_update_index(client, index_name): 
    fields = [ 
        SimpleField(name="id", type=SearchFieldDataType.String, key=True), 
        SearchField( 
            name="text", 
            type=SearchFieldDataType.String, 
            searchable=True, 
        ), 
        SearchField( 
            name="embedding", 
            type="Collection(Edm.SByte)", 
            vector_search_dimensions=1024, 
            vector_search_profile_name="my-vector-config",  
        ), 
    ] 
    vector_search = VectorSearch( 
        profiles=[ 
            VectorSearchProfile( 
                name="my-vector-config", 
                algorithm_configuration_name="my-hnsw", 
            ) 
        ], 
        algorithms=[ 
            HnswAlgorithmConfiguration( 
                name="my-hnsw", 
                kind=VectorSearchAlgorithmKind.HNSW, 
            ) 
        ], 
    ) 
    index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search) 
    client.create_or_update_index(index=index)

索引文档及其嵌入

将文档及其 int8 嵌入索引到 Azure AI 搜索中。

def index_documents(search_client, documents, embeddings): 
    documents_to_index = [ 
        {"id": str(idx), "text": doc, "embedding": emb} 
        for idx, (doc, emb) in enumerate(zip(documents, embeddings)) 
    ] 
    search_client.upload_documents(documents=documents_to_index)

运行工作流程

运行上述步骤来生成嵌入、创建搜索索引并上传文档。

documents = [ 
    "Alan Turing  was an English mathematician, computer scientist, logician, cryptanalyst, philosopher and theoretical biologist.", 
    "Albert Einstein was a German-born theoretical physicist who is widely held to be one of the greatest and most influential scientists of all time.", 
    "Isaac Newton was an English polymath active as a mathematician, physicist, astronomer, alchemist, theologian, and author who was described in his time as a natural philosopher.", 
    "Marie Curie was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity" 
] 

# Generate embeddings 
embeddings = generate_embeddings(documents) 

# Initialize Azure Search Index Client 
search_index_client = SearchIndexClient( 
    endpoint=search_service_endpoint, 
    credential=credential, 
    index_name=index_name 
)

# Create or update the search index to include the embedding field 
create_or_update_index(search_index_client, index_name) 

# Initialize the SearchClient 
search_client = SearchClient( 
    endpoint=search_service_endpoint,  
    index_name=index_name,  
    credential=credential 
) 
# Index the documents and their embeddings 
index_documents(search_client, documents, embeddings)

执行矢量搜索

使用 Azure AI 搜索客户端使用生成的嵌入执行矢量搜索。

# Query for vector search 
query = "foundational figures in computer science" 

# Generate query embeddings 
# Use input_type="search_query" for query embeddings 
query_embeddings = generate_embeddings(query, input_type="search_query") 
search_client = SearchClient(search_service_endpoint, index_name, credential) 
vector_query = VectorizedQuery( 
    vector=query_embeddings[0], k_nearest_neighbors=3, fields="embedding" 
) 
results = search_client.search( 
    search_text=None,  # No search text for pure vector search 
    vector_queries=[vector_query], 
) 
for result in results: 
    print(f"Text: {result['text']}") 
    print(f"Score: {result['@search.score']}\n") 
Title: Alan Turing  was an English mathematician, computer scientist, logician, cryptanalyst, philosopher and theoretical biologist. 
Score: 0.62440896 

Title: Albert Einstein was a German-born theoretical physicist who is widely held to be one of the greatest and most influential scientists of all time. 
Score: 0.59141135 

Title: Marie Curie was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity 
Score: 0.57616836