跳到主要内容

oai.cohere

使用Cohere的API创建一个与OpenAI兼容的客户端。

示例:

llm_config={

  • "config_list" - [{

  • "api_type" - "cohere",

  • "model" - "command-r-plus",

  • "api_key" - os.environ.get("COHERE_API_KEY")

  • "client_name" - "autogen-cohere", # 可选参数 } ]}

    agent = autogen.AssistantAgent("my_agent", llm_config=llm_config)

    使用以下命令安装Cohere的python库:pip install --upgrade cohere

    资源:

CohereClient

class CohereClient()

Cohere API的客户端。

__init__

def __init__(**kwargs)

需要设置api_key或环境变量

参数:

  • api_key str - 用于使用Cohere的API密钥(或需要设置环境变量COHERE_API_KEY)

消息检索

def message_retrieval(response) -> List

从响应中检索并返回一个字符串列表或一个Choice.Message列表。

注意:如果返回的是一个Choice.Message列表,它目前需要包含OpenAI的ChatCompletion Message对象的字段,因为在当前代码库中,除非使用自定义代理,否则这是函数或工具调用的预期。

get_usage

@staticmethod
def get_usage(response) -> Dict

使用 RESPONSE_USAGE_KEYS 返回响应的使用摘要。

parse_params

def parse_params(params: Dict[str, Any]) -> Dict[str, Any]

从传入的参数中加载Cohere API的参数,并返回一组经过验证的参数。检查类型、范围,并设置默认值。

oai_messages_to_cohere_messages

def oai_messages_to_cohere_messages(
messages: list[Dict[str, Any]], params: Dict[str, Any],
cohere_params: Dict[str,
Any]) -> tuple[list[dict[str, Any]], str, str]

将消息从OAI格式转换为Cohere的格式。 我们校正任何特定的角色顺序和类型。

参数:

  • messages - list[Dict[str, Any]]: AutoGen 消息
  • params - Dict[str, Any]: AutoGen参数字典
  • cohere_params - Dict[str, Any]: Cohere参数字典

返回:

List[Dict[str, Any]]: 聊天历史消息

  • str - 前言(系统消息)
  • str - 消息(最终用户的消息)

calculate_cohere_cost

def calculate_cohere_cost(input_tokens: int, output_tokens: int,
model: str) -> float

根据Cohere定价计算完成成本。

CohereError

class CohereError(Exception)

其他Cohere异常的基础类

CohereRateLimitError

class CohereRateLimitError(CohereError)

当超过速率限制时引发