Skip to content

Commit

Permalink
update integration code snippets (#211)
Browse files Browse the repository at this point in the history
Co-authored-by: trentfowlercohere <[email protected]>
  • Loading branch information
mrmer1 and trentfowlercohere authored Oct 24, 2024
1 parent 0d1c3db commit 8ed14b0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ llm = ChatCohere(cohere_api_key="COHERE_API_KEY",

# Send a chat message without chat history
current_message = [HumanMessage(content="knock knock")]
print(llm(current_message))
print(llm.invoke(current_message))

# Send a chat message with chat history, note the last message is the current user message
current_message_and_history = [
HumanMessage(content="knock knock"),
AIMessage(content="Who's there?"),
HumanMessage(content="Tank") ]
print(llm(current_message_and_history))
print(llm.invoke(current_message_and_history))
```

### Cohere Agents with LangChain
Expand Down
89 changes: 65 additions & 24 deletions fern/pages/integrations/llamaindex.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ updatedAt: 'Tue May 14 2024 15:10:49 GMT+0000 (Coordinated Universal Time)'

To use LlamaIndex and Cohere, you will need:

- LlamaIndex Package. To install it, run `pip install llama-index`, `llama-index-llms-cohere`, and `llama-index-embeddings-cohere`.
- LlamaIndex Package. To install it, run:
- `pip install llama-index`
- `pip install llama-index-llms-cohere` (to use the Command models)
- `pip install llama-index-embeddings-cohere` (to use the Embed models)
- `pip install llama-index-postprocessor-cohere-rerank` (to use the Rerank models)
- Cohere's SDK. To install it, run `pip install cohere`. If you run into any issues or want more details on Cohere's SDK, [see this wiki](https://github.com/cohere-ai/cohere-python).
- A Cohere API Key. For more details on pricing [see this page](https://cohere.com/pricing). When you create an account with Cohere, we automatically create a trial API key for you. This key will be available on the dashboard where you can copy it, and it's in the dashboard section called "API Keys" as well.

Expand All @@ -26,10 +30,13 @@ To use Cohere's chat functionality with LlamaIndex create a [Cohere model object
from llama_index.llms.cohere import Cohere
from llama_index.core.llms import ChatMessage

cohere_model = Cohere(api_key=os.getenv("COHERE_API_KEY"))
cohere_model = Cohere(api_key="COHERE_API_KEY",
model="command-r-plus")

message = ChatMessage(role="user", content= "What is 2 + 3?")
resp = cohere_model.chat([message])
print(resp)

response = cohere_model.chat([message])
print(response)
```

### Cohere Embeddings with LlamaIndex
Expand All @@ -40,9 +47,9 @@ To use Cohere's embeddings with LlamaIndex create a [Cohere Embeddings object](h
from llama_index.embeddings.cohere import CohereEmbedding

embed_model = CohereEmbedding(
cohere_api_key=cohere_api_key,
model_name="embed-english-v3.0", # Supports all Cohere embed models
input_type="search_query", # Required for v3 models
api_key="COHERE_API_KEY",
model_name="embed-english-v3.0",
input_type="search_document", # Use search_query for queries, search_document for documents
)

# Generate Embeddings
Expand All @@ -58,47 +65,81 @@ print(embeddings[:5])
To use Cohere's rerank functionality with LlamaIndex create a [ Cohere Rerank object ](https://docs.llamaindex.ai/en/latest/examples/node_postprocessor/CohereRerank.html#) and use as a [node post processor.](https://docs.llamaindex.ai/en/stable/module_guides/querying/node_postprocessors/root.html)

```python PYTHON
cohere_rerank = CohereRerank(api_key="{API_KEY}", top_n=2)
from llama_index.postprocessor.cohere_rerank import CohereRerank
from llama_index.readers.web import SimpleWebPageReader # first, run `pip install llama-index-readers-web`

# create index (we are using an example page from Cohere's docs)
documents = SimpleWebPageReader(html_to_text=True).load_data(
["https://docs.cohere.com/v2/docs/prompt-tuner"]
) # you can replace this with any other reader or documents
index = VectorStoreIndex.from_documents(documents=documents)

# create reranker
cohere_rerank = CohereRerank(api_key="COHERE_API_KEY",
model="rerank-english-v3.0",
top_n=2)

# query the index
query_engine = index.as_query_engine(
similarity_top_k=10,
node_postprocessors=[cohere_rerank],
)

print(query_engine)

# generate a response
response = query_engine.query(
"What is Cohere Prompt Tuner?",
)

print(response)

# To view the source documents
from llama_index.core.response.pprint_utils import pprint_response
pprint_response(response, show_source=True)
```

### Cohere RAG with LlamaIndex

The following example uses Cohere's chat model, embeddings and rerank functionality to generate a response based on your data.

```python PYTHON
# rerank

from llama_index import ServiceContext, VectorStoreIndex
from llama_index.llms.cohere import Cohere
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
from llama_index.embeddings.cohereai import CohereEmbedding
from llama_index.embeddings.cohere import CohereEmbedding
from llama_index.postprocessor.cohere_rerank import CohereRerank
from llama_index.core import Settings
from llama_index.core import VectorStoreIndex
from llama_index.readers.web import SimpleWebPageReader # first, run `pip install llama-index-readers-web`

# Create the embedding model
embed_model = CohereEmbedding(
cohere_api_key="{API_KEY}",
api_key="COHERE_API_KEY",
model_name="embed-english-v3.0",
input_type="search_query",
)

# Create the service context with the cohere model for generation and embedding model
service_context = ServiceContext.from_defaults(
llm=Cohere(api_key="{API_KEY}", model="command"),
embed_model=embed_model
)
Settings.llm = Cohere(api_key="COHERE_API_KEY",
model="command-r-plus")
Settings.embed_model = embed_model

# Load the data, for this example data needs to be in a test file
data = SimpleDirectoryReader(input_files=["example_data_file.txt"]).load_data()
index = VectorStoreIndex.from_documents(data, service_context=service_context)
# create index (we are using an example page from Cohere's docs)
documents = SimpleWebPageReader(html_to_text=True).load_data(
["https://docs.cohere.com/v2/docs/prompt-tuner"]
) # you can replace this with any other reader or documents
index = VectorStoreIndex.from_documents(documents=documents)

# Create a cohere reranker
cohere_rerank = CohereRerank(api_key="{API_KEY}")
cohere_rerank = CohereRerank(api_key="COHERE_API_KEY",
model="rerank-english-v3.0",
top_n=2)

# Create the query engine
query_engine = index.as_query_engine(node_postprocessors=[cohere_rerank])

# Generate the response
response = query_engine.query("Who founded Cohere?",)
response = query_engine.query("What is Cohere Prompt Tuner?")
print(response)
```

### Cohere Tool Use (Function Calling) with LlamaIndex
Expand All @@ -124,7 +165,7 @@ def add(a: int, b: int) -> int:
add_tool = FunctionTool.from_defaults(fn=add)

# Define LLM
llm = Cohere(api_key=os.getenv("COHERE_API_KEY"),
llm = Cohere(api_key="COHERE_API_KEY",
model="command-r-plus")

# Create agent
Expand Down

0 comments on commit 8ed14b0

Please sign in to comment.