Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: example with cohere and llamacloud #184

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/cohere-llamaparse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# cohere-llamaparse

This examples tests a document parsed by [LlamaCloud](https://cloud.llamaindex.ai/login) and Cohere's Command R+ model with document searching.

## Usage

```
poetry install
```

Run the Empirical while defining the env variables `LLAMA_CLOUD_API_KEY` and `CO_API_KEY`.

```
LLAMA_CLOUD_API_KEY=foo CO_API_KEY=bar npx @empiricalrun/cli run --python-path `poetry env info -e`
```
34 changes: 34 additions & 0 deletions examples/cohere-llamaparse/cohere-async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import httpx
import os


async def execute(inputs, parameters):
client = httpx.AsyncClient()
response = await client.get(
'https://api.cloud.llamaindex.ai/api/parsing/job/4763f2d1-5fe5-434e-abb7-b18b3e8d2577/result/markdown',
headers={
'accept': 'application/json',
'Authorization': f'Bearer {os.environ.get("LLAMA_CLOUD_API_KEY", "")}'
}
)
parsed_response = response.json()
doc_content = parsed_response['markdown']
response = await client.post(
'https://api.cohere.ai/v1/chat',
timeout=30.0, # default 10 is not working
headers={
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.environ.get("CO_API_KEY", "")}'
},
json={
"preamble": "You are a helpful assistant for a gaming payments company that connects merchants (gaming apps) with their users. Users pay on the gaming apps, and this company processes their payments\n\nUsers need help regarding their transactions and have questions that they will ask you. You need to represent this company and their merchant, and talk to the user in a helpful and polite tone.\n\nUse the documents that you have to find answers, instead of jumping to answer the question yourself.",
"message": inputs["question"],
"documents": [{"title": "Client Operations.pdf", "text": doc_content}],
}
)
result = response.json()
await client.aclose()
return {
"value": result["text"]
}
31 changes: 31 additions & 0 deletions examples/cohere-llamaparse/cohere.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import requests
import os


def execute(inputs, parameters):
response = requests.get(
'https://api.cloud.llamaindex.ai/api/parsing/job/4763f2d1-5fe5-434e-abb7-b18b3e8d2577/result/markdown',
headers={
'accept': 'application/json',
'Authorization': f'Bearer {os.environ.get("LLAMA_CLOUD_API_KEY", "")}'
}
)
parsed_response = response.json()
doc_content = parsed_response['markdown']
response = requests.post(
'https://api.cohere.ai/v1/chat',
headers={
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.environ.get("CO_API_KEY", "")}'
},
json={
"preamble": "You are a helpful assistant for a gaming payments company that connects merchants (gaming apps) with their users. Users pay on the gaming apps, and this company processes their payments\n\nUsers need help regarding their transactions and have questions that they will ask you. You need to represent this company and their merchant, and talk to the user in a helpful and polite tone.\n\nUse the documents that you have to find answers, instead of jumping to answer the question yourself.",
"message": inputs["question"],
"documents": [{"title": "Client Operations.pdf", "text": doc_content}],
}
)
result = response.json()
return {
"value": result["text"]
}
23 changes: 23 additions & 0 deletions examples/cohere-llamaparse/empiricalrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://assets.empirical.run/config/schema/latest.json",
"runs": [
{
"type": "py-script",
"path": "cohere.py"
},
{
"type": "py-script",
"path": "cohere-async.py"
}
],
"dataset": {
"path": "https://docs.google.com/spreadsheets/d/1U8fBQ9TxtR5pUS0Bg0n1xcU_6EXkhGUOj1CS-TAAHmU/edit#gid=0"
},
"scorers": [
{
"type": "llm-critic",
"criteria": "{{ success criteria }}",
"name": "success-criteria"
}
]
}
Loading