Cohere

Cohere

출처: 문서

본문

기본 사용법 (Basic Usage)

프롬프트로 complete 호출

Colab에서 이 노트북을 여는 경우라면 LlamaIndex 🦙 설치가 필요할 거예요.

%pip install llama-index-llms-openai
%pip install llama-index-llms-cohere
!pip install llama-index
from llama_index.llms.cohere import Cohere


api_key = "Your api key"
resp = Cohere(api_key=api_key).complete("Paul Graham is ")
Your text contains a trailing whitespace, which has been trimmed to ensure high quality generations.
print(resp)
an English computer scientist, entrepreneur and investor. He is best known for his work as a co-founder of the seed accelerator Y Combinator. He is also the author of the free startup advice blog "Startups.com". Paul Graham is known for his philanthropic efforts. Has given away hundreds of millions of dollars to good causes.

메시지 목록으로 chat 호출

from llama_index.core.llms import ChatMessage
from llama_index.llms.cohere import Cohere


messages = [
    ChatMessage(role="user", content="hello there"),
    ChatMessage(
        role="assistant", content="Arrrr, matey! How can I help ye today?"
    ),
    ChatMessage(role="user", content="What is your name"),
]


resp = Cohere(api_key=api_key).chat(
    messages, preamble_override="You are a pirate with a colorful personality"
)
print(resp)
assistant: Traditionally, ye refers to gender-nonconforming people of any gender, and those who are genderless, whereas matey refers to a friend, commonly used to address a fellow pirate. According to pop culture in works like "Pirates of the Carribean", the romantic interest of Jack Sparrow refers to themselves using the gender-neutral pronoun "ye".


Are you interested in learning more about the pirate culture?

스트리밍 (Streaming)

stream_complete 엔드포인트 사용

from llama_index.llms.cohere import Cohere


llm = Cohere(api_key=api_key)
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
    print(r.delta, end="")
 an English computer scientist, essayist, and venture capitalist. He is best known for his work as a co-founder of the Y Combinator startup incubator, and his essays, which are widely read and influential in the startup community.

stream_chat 엔드포인트 사용

from llama_index.llms.openai import OpenAI


llm = Cohere(api_key=api_key)
messages = [
    ChatMessage(role="user", content="hello there"),
    ChatMessage(
        role="assistant", content="Arrrr, matey! How can I help ye today?"
    ),
    ChatMessage(role="user", content="What is your name"),
]
resp = llm.stream_chat(
    messages, preamble_override="You are a pirate with a colorful personality"
)
for r in resp:
    print(r.delta, end="")
Arrrr, matey! According to etiquette, we are suppose to exchange names first! Mine remains a mystery for now.

모델 구성 (Configure Model)

from llama_index.llms.cohere import Cohere


llm = Cohere(model="command", api_key=api_key)
resp = llm.complete("Paul Graham is ")
Your text contains a trailing whitespace, which has been trimmed to ensure high quality generations.
print(resp)
an English computer scientist, entrepreneur and investor. He is best known for his work as a co-founder of the seed accelerator Y Combinator. He is also the co-founder of the online dating platform Match.com.

비동기 (Async)

from llama_index.llms.cohere import Cohere


llm = Cohere(model="command", api_key=api_key)
resp = await llm.acomplete("Paul Graham is ")
Your text contains a trailing whitespace, which has been trimmed to ensure high quality generations.
print(resp)
an English computer scientist, entrepreneur and investor. He is best known for his work as a co-founder of the startup incubator and seed fund Y Combinator, and the programming language Lisp. He has also written numerous essays, many of which have become highly influential in the software engineering field.
resp = await llm.astream_complete("Paul Graham is ")
async for delta in resp:
    print(delta.delta, end="")
 an English computer scientist, essayist, and businessman. He is best known for his work as a co-founder of the startup accelerator Y Combinator, and his essay "Beating the Averages."

API 키를 인스턴스 단위로 설정하기

원한다면 별도의 LLM 인스턴스가 별도의 API 키를 사용하게 할 수 있어요.

from llama_index.llms.cohere import Cohere


llm_good = Cohere(api_key=api_key)
llm_bad = Cohere(model="command", api_key="BAD_KEY")


resp = llm_good.complete("Paul Graham is ")
print(resp)


resp = llm_bad.complete("Paul Graham is ")
print(resp)
Your text contains a trailing whitespace, which has been trimmed to ensure high quality generations.




an English computer scientist, entrepreneur and investor. He is best known for his work as a co-founder of the acceleration program Y Combinator. He has also written extensively on the topics of computer science and entrepreneurship. Where did you come across his name?

잘못된 API 키를 사용한 두 번째 호출은 CohereAPIError: invalid api token 예외를 발생시켜요.

더 알아보기 (Learn more)