Skip to main content
Cohere is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions. This will help you getting started with ChatCohere chat models. For detailed documentation of all ChatCohere features and configurations head to the API reference.

概述

集成详情

模型功能

请参阅下表标题中的链接,了解如何使用特定功能。

设置

In order to use the LangChain.js Cohere integration you’ll need an API key. You can sign up for a Cohere account and create an API key. You’ll first need to install the @langchain/cohere package.

凭证

前往 Cohere’s website 注册 Cohere 并生成 API 密钥。完成后设置 COHERE_API_KEY 环境变量:
如果你想要自动追踪模型调用,还可以设置你的 LangSmith API 密钥,取消注释以下内容:

安装

LangChain 的 ChatCohere 集成位于 @langchain/cohere 包中:

实例化

现在我们可以实例化模型对象并生成聊天补全:

Custom client for Cohere on Azure, Cohere on AWS Bedrock, and standalone Cohere instance

We can instantiate a custom CohereClient and pass it to the ChatCohere constructor. Note: If a custom client is provided both COHERE_API_KEY environment variable and apiKey parameter in the constructor will be ignored.

调用

RAG

Cohere also comes out of the box with RAG support. You can pass in documents as context to the API request and Cohere’s models will use them when generating responses.

Connectors

The API also allows for other connections which are not static documents. An example of this is their web-search connector which allows you to pass in a query and the API will search the web for relevant documents. The example below demonstrates how to use this feature.
We can see in the additional_kwargs object that the API request did a few things:
  • Performed a search query, storing the result data in the searchQueries and searchResults fields. In the searchQueries field we see they rephrased our query for better results.
  • Generated three documents from the search query.
  • Generated a list of citations
  • Generated a final response based on the above actions & content.

API 参考

有关所有 ChatCohere 功能和配置的详细文档,请前往 API 参考