-
Notifications
You must be signed in to change notification settings - Fork 106
/
cohere_example.py
47 lines (38 loc) · 1.75 KB
/
cohere_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import cohere
from traceloop.sdk import Traceloop
from traceloop.sdk.decorators import workflow
Traceloop.init()
co = cohere.Client(os.environ.get("COHERE_API_KEY"))
@workflow(name="pirate_joke_generator")
def joke_workflow():
return co.chat(model="command", message="Tell me a joke, pirate style")
@workflow(name="rerank")
def rerank():
query = "What is the capital of the United States?"
documents = [
"Carson City is the capital city of the American state of Nevada."
+ " At the 2010 United States Census, Carson City had a population of 55,274.",
"The Commonwealth of the Northern Mariana Islands is a group of islands"
+ " in the Pacific Ocean that are a political division controlled by the "
+ "United States. Its capital is Saipan.",
"Charlotte Amalie is the capital and largest city of the United States "
+ "Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
"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. ",
"Capital punishment (the death penalty) has existed in the United States "
+ "since before the United States was a country. As of 2017, capital "
+ "punishment is legal in 30 of the 50 states.",
"North Dakota is a state in the United States. 672,591 people lived"
+ " in North Dakota in the year 2010. The capital and seat of government is Bismarck.",
]
results = co.rerank(
query=query,
documents=documents,
top_n=3,
model="rerank-multilingual-v2.0",
)
print(results)
# res = joke_workflow()
# print(res)
rerank()