Skip to content

Instantly share code, notes, and snippets.

@nreimers
Last active April 26, 2023 14:55
Show Gist options
  • Save nreimers/f24c5cf7569279a14caab246d468c1af to your computer and use it in GitHub Desktop.
Save nreimers/f24c5cf7569279a14caab246d468c1af to your computer and use it in GitHub Desktop.
# 1) Install dependencies: pip install cohere
# 2) Get your Cohere API key and past it below
import cohere
# Get your cohere API key on: www.cohere.com
co = cohere.Client("<<YOUR_COHERE_API_KEY>>>")
# Example query and passages
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-02")
for result in results:
print(f"Doc #{result.index}, Relevance: {result.relevance_score:.2f}")
print(result.document['text'])
print("----")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment