现在我们已经安装了 cohere 库并获得了 API 密钥,就可以发起 API 调用。以下代码中,我们:
导入 cohere 库以与其 API 交互。
使用我们的 API 密钥创建一个 Client 对象。
定义一个要发送给 LLM 的提示。
使用 co.chat() 调用模型。
提取响应对象并打印。
import cohere
co = cohere.Client('your_token_here')
message ="What is Machine Learning?"
response = co.chat(
message=message,
model="command",
temperature=0.3)
answer = response.text
print(answer)技术支持
Machine learning is a branch of computer science that focuses on developing algorithms and models that enable computers to learn and make predictions or decisions without being explicitly programmed. It is a key component of artificial intelligence (AI).
Machine Learning allows systems to identify patterns in data, uncover insights, and improve processes through iterative learning. Instead of writing explicit programs, ML enables the creation of models that can analyze data, identify patterns, and predict outcomes. These models are refined and optimized through a process known as training, which involves feeding the models annotated examples or real-world data.
(...)技术支持
在多轮对话中,保留先前交互的上下文对生成连贯且相关的响应至关重要。Cohere 的 API 通过允许我们在 chat_history 参数中包含聊天历史来提供支持。
这有助于模型理解对话走向,并基于先前交流做出恰当回应。我们来看一个示例。
message ="How is this related to Artificial Intelligence?"
response = co.chat(
model="command-r-plus",
chat_history=[{"role":"USER","text":"What is Large Language Model?"},{"role":"CHATBOT","text":"Large Language Model is a type ..."},],
message="How is this related to Artificial Intelligence?")print(response.text)技术支持
Large language models (LLMs) are a key component of artificial intelligence (AI), and their development has played a significant role in advancing the field of AI. Here's how they are related:
1. Language Understanding and Generation: AI aims to create intelligent systems that can understand and respond to human language. LLMs are designed to process and generate human-like language, which is a fundamental aspect of human intelligence. By training on vast amounts of text data, LLMs learn to recognize patterns, syntax, semantics, and context, enabling them to interpret and generate language in a way that is similar to how humans do.
2. Machine Learning and Neural Networks: LLMs are typically built using machine learning techniques, particularly deep learning and neural networks. These are core areas of AI research. Neural networks, inspired by the structure of the human brain, enable LLMs to learn and make connections between different pieces of information. By training on large datasets, LLMs can identify patterns, relationships, and meanings in the data, which is a fundamental aspect of AI.
3. (...)技术支持
当用户询问 "How is this related to Artificial Intelligence?" 时,模型理解到其中的 "this" 指的是大型语言模型(LLMs)。这得益于 chat_history 参数提供的前序交互上下文。在多轮对话中维护上下文对于生成相关且连贯的回答至关重要。
这种方式很好,但像上例那样手动将 chat_history 传给 API 并不现实。我们可以不硬编码 chat_history,而是在对话过程中动态维护。
实现方式有多种。最简单的是在 API 中使用 conversation_id。来看示例:
# First message
response = co.chat(
model="command-r-plus",
message="What is Large Language Models?",
conversation_id='newuser1',)print(response.text)技术支持
Large Language Models (LLMs) are a type of artificial intelligence model specifically designed to process and generate human language. These models are typically based on deep learning and neural network architectures and are trained on vast amounts of text data. Here's an overview of LLMs and their key characteristics:
1. Definition:
- Large Language Models are machine learning models that are trained on large-scale textual data to understand, generate, and manipulate human language. They aim to capture the complexities of language, including syntax, semantics, and context.
2. Training Data:
- LLMs are trained on massive amounts of text data, often consisting of billions or even trillions of words. This training data can come from various sources such as books, articles, websites, social media, and other text-based documents.
3. (...)技术支持
# Second message
response = co.chat(
model="command-r-plus",
message="How is it related to Artificial Intelligence?",
conversation_id='newuser1',)print(response.text)技术支持
Large Language Models (LLMs) are a crucial component and a powerful manifestation of Artificial Intelligence (AI). Here's how LLMs are closely related to AI:
1. AI Foundation:
- Artificial Intelligence is a broad field that encompasses the development of computer systems capable of performing tasks typically associated with human intelligence. This includes areas like machine learning, natural language processing, computer vision, robotics, and decision-making.
- LLMs fall under the umbrella of AI as they are designed to understand, generate, and respond to human language, which is a fundamental aspect of human intelligence.
2. Machine Learning:
- Machine learning is a subset of AI that focuses on the development of algorithms and models that enable computers to learn and improve over time, based on data and experience, without being explicitly programmed.
- LLMs are built using machine learning techniques, particularly deep learning and neural networks. They are trained on large datasets to identify patterns, understand context, and generate responses, demonstrating their ability to learn and improve from data.
3. (...)技术支持
在瞬息万变的人工智能领域,大型语言模型的到来彻底改变了我们与文本内容交互和生成的方式。自 2022 年 11 月 OpenAI 推出 ChatGPT 以来,我们每天都能看到新的大型语言模型发布。
在这些模型中,Cohere Command 已成为实践者的热门之选。Cohere 提供前沿工具,帮助开发者和企业在其应用与用例中充分利用基础模型的能力。
本初学者指南旨在介绍 Cohere 的 API 产品,深入探讨其功能、优势以及实用的落地策略。
我们将从大型语言模型的快速概览开始;如果您想要更系统的学习,请查看这门关于大型语言模型(LLMs)的课程。
Cohere 与 LLMs
大型语言模型(LLMs)是能够理解并生成类人文本的高级 AI 系统。它们在海量数据上训练,以把握复杂模式、理解语言细微差别,并生成连贯的回应。
LLMs 可执行多种与语言相关的任务,包括翻译、文本补全、摘要以及对话交互。
Cohere 是该领域的重要参与者,提供易用的 API,让数据科学家能够便捷获取这些 LLM 能力,从而简化工作流程。
什么是 Cohere
Cohere 成立于 2019 年,是一家总部位于加拿大的企业,致力于为企业打造人工智能解决方案。它迅速成为大型语言模型(LLM)行业的重要力量,常与 OpenAI GPT 和 Anthropic Claude 等竞争对手并列提及。
来源:Cobus Greyling,Large Language Model Landscape,Medium
Cohere 主要开发并提供用于语言生成的基础模型,开发者和企业可主要通过其 API 服务进行访问。
Cohere 主要提供三类不同的模型:
我们可以通过两种方式访问 Cohere 模型:
Cohere 还提供类似 ChatGPT 的专用聊天界面——其底层由 Command R+ 提供支持。
Cohere Playground
Cohere Playground 提供了与 Cohere 模型交互的友好界面,体验直观,类似于使用 GPT。
用户可以探索并试验多种模型能力,生成文本并洞察其行为。凭借简洁易用的设计,Cohere Playground 让用户能够快速原型化并测试 Cohere 的模型产品。
Cohere Playground 对用户免费开放用于探索与试验模型(在投产之前免费)。
要使用 Cohere Playground,我们首先需要在Cohere 官网注册。
随后登录,我们将看到如下页面——与 OpenAI 平台的 playground 相当相似。使用右侧的下拉菜单,我们可以选择不同的 Cohere 模型。
Playground 针对不同任务有略微不同的界面。在顶部,您可以从四个选项中进行选择:Chat、Classify、Embed 和 Generate。
Cohere Playground 仪表盘
Cohere Playground 非常适合快速测试 Cohere 模型的能力,但如果您要构建应用或工作流,就需要以编程方式访问这些模型。这正是 Cohere API 的用武之地!
Cohere API
访问 Cohere 的 API 相当直接。概括来说,我们需要:
我们逐步讲解这些步骤。
安装 Cohere 库
我们可以使用
pip安装cohere客户端。创建 API 密钥
现在,我们需要一个 API 密钥。
如下图所示,API 密钥有两种类型:生产和试用。对于本教程,我们只需要试用密钥。点击密钥旁的复制图标即可复制。
发起一次 API 调用
现在我们已经安装了
cohere库并获得了 API 密钥,就可以发起 API 调用。以下代码中,我们:cohere库以与其 API 交互。Client对象。co.chat()调用模型。响应结构
前面我们通过
response.text获取了答案。但响应对象包含更多信息,我们可以使用response.dict()访问——它将返回一个 JSON 文件,包含包括响应消息在内的所有元数据:我们来拆解一下
response对象的各字段:text:包含模型生成的消息。generation_id:包含与生成答案关联的 ID,可与 Feedback API 端点配合使用,以标注优质回答并报告较差回答。finish_reason:指示生成结束的原因,可能为以下之一:COMPLETE:模型成功完成消息生成。MAX_TOKENS:在生成完成前达到模型上下文限制。chat_history:以聊天日志格式呈现的对话内容。meta:包含关于 token 计数、计费等信息。response_id:generation_id关注一次文本生成尝试,而response_id标识包含该生成文本及其他相关数据的整个响应对象。Cohere API 中的多轮对话
设想您正在开发一个用于回答用户问题的聊天机器人。保留先前对话的上下文,可使语言模型提供更相关的回答。
在多轮对话中,保留先前交互的上下文对生成连贯且相关的响应至关重要。Cohere 的 API 通过允许我们在
chat_history参数中包含聊天历史来提供支持。这有助于模型理解对话走向,并基于先前交流做出恰当回应。我们来看一个示例。
当用户询问 "How is this related to Artificial Intelligence?" 时,模型理解到其中的 "this" 指的是大型语言模型(LLMs)。这得益于
chat_history参数提供的前序交互上下文。在多轮对话中维护上下文对于生成相关且连贯的回答至关重要。这种方式很好,但像上例那样手动将
chat_history传给 API 并不现实。我们可以不硬编码chat_history,而是在对话过程中动态维护。实现方式有多种。最简单的是在 API 中使用
conversation_id。来看示例:现在我们再向模型发送另一条消息——注意
conversation_id与第一条消息保持一致。只要conversation_id保持一致,API 就会自动将之前的消息作为上下文使用。如果您想继续对话,需要保持相同的 conversation_id,API 会自动记住之前的所有对话。
conversation_id不应与chat_history同时使用,它们是互斥的。如需了解更多,请查看Cohere 的文档。
Cohere 定价
我们来看看 Cohere 最先进模型的定价,并与 OpenAI 与 Anthropic 的顶级专有语言模型进行比较(我们只关注各家公司最强的模型)。截至 2024 年 5 月,价格如下:
模型
$/百万输入 token
$/百万输出 token
Cohere Command R+
$3.00
$15.00
Anthropic Claude Opus
$15.00
$75.00
OpenAI GPT-4 Turbo
$10.00
$30.00
尽管 Cohere Command R+ 在成本上具有吸引力,但也需要结合这些模型在更广泛维度上的表现来综合考量。Anthropic Claude Opus 和 GPT-4 Turbo 在基准测试中一直表现更强。
结语
无论您是希望增强应用的开发者,还是计划引入前沿 AI 的企业,Cohere 都提供可与 GPT 与 Claude 等其他基础模型竞争的大模型——且价格显著更低。
在本文中,我们了解了什么是 Cohere,以及如何通过 Playground 或 API 进行访问。
如果您想进一步探索,推荐这段代码跟练:使用 Cohere API 操作大型语言模型。
如果您希望更进一步,学习如何开发端到端 AI 应用,请查看使用 LangChain 开发 LLM 应用。