Skip to content

Commit bf1ee17

Browse files
committed
add examples for v2 client for cohere
1 parent 45ae162 commit bf1ee17

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
from examples.cohere_example.chat import chat_comp
2+
from examples.cohere_example.chatv2 import chat_v2
3+
from examples.cohere_example.chat_streamv2 import chat_stream_v2
24
from examples.cohere_example.chat_stream import chat_stream
35
from examples.cohere_example.tools import tool_calling
46
from examples.cohere_example.embed import embed
57
from examples.cohere_example.rerank import rerank
8+
from examples.cohere_example.rerankv2 import rerank_v2
69
from langtrace_python_sdk import with_langtrace_root_span
710

811

912
class CohereRunner:
1013

1114
@with_langtrace_root_span("Cohere")
1215
def run(self):
16+
chat_v2()
17+
chat_stream_v2()
1318
chat_comp()
1419
chat_stream()
1520
tool_calling()
1621
embed()
1722
rerank()
23+
rerank_v2()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from langtrace_python_sdk import langtrace
3+
import cohere
4+
5+
langtrace.init(api_key=os.getenv("LANGTRACE_API_KEY"))
6+
co = cohere.ClientV2(api_key=os.getenv("COHERE_API_KEY"))
7+
8+
def chat_stream_v2():
9+
res = co.chat_stream(
10+
model="command-r-plus-08-2024",
11+
messages=[{"role": "user", "content": "Write a title for a blog post about API design. Only output the title text"}],
12+
)
13+
14+
for event in res:
15+
if event:
16+
if event.type == "content-delta":
17+
print(event.delta.message.content.text)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
from langtrace_python_sdk import langtrace
3+
import cohere
4+
5+
langtrace.init(api_key=os.getenv("LANGTRACE_API_KEY"))
6+
7+
8+
def chat_v2():
9+
co = cohere.ClientV2(api_key=os.getenv("COHERE_API_KEY"))
10+
11+
res = co.chat(
12+
model="command-r-plus-08-2024",
13+
messages=[
14+
{
15+
"role": "user",
16+
"content": "Write a title for a blog post about API design. Only output the title text.",
17+
}
18+
],
19+
)
20+
21+
print(res.message.content[0].text)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
from langtrace_python_sdk import langtrace
3+
import cohere
4+
5+
langtrace.init(api_key=os.getenv("LANGTRACE_API_KEY"))
6+
co = cohere.ClientV2(api_key=os.getenv("COHERE_API_KEY"))
7+
8+
docs = [
9+
"Carson City is the capital city of the American state of Nevada.",
10+
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
11+
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
12+
"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
13+
"Capital punishment has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
14+
]
15+
16+
def rerank_v2():
17+
response = co.rerank(
18+
model="rerank-v3.5",
19+
query="What is the capital of the United States?",
20+
documents=docs,
21+
top_n=3,
22+
)
23+
print(response)

src/run_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"anthropic": False,
55
"azureopenai": False,
66
"chroma": False,
7-
"cohere": False,
7+
"cohere": True,
88
"fastapi": False,
99
"langchain": False,
1010
"llamaindex": False,
@@ -21,7 +21,7 @@
2121
"gemini": False,
2222
"mistral": False,
2323
"awsbedrock": False,
24-
"cerebras": True,
24+
"cerebras": False,
2525
}
2626

2727
if ENABLED_EXAMPLES["anthropic"]:

0 commit comments

Comments
 (0)