Getting started

Chat with datasources

With Superagent you can easilly create an AI Assistant that has access to your private data thorugh a conecpt we call Datasources. Superagent supports different types of datasources listed here. Attaching a datasource to an Assistant enables that Assistant to read and analyze that data.

Step-by-step guide

Note that you usually only need to create the llm object once and re-use it for subsequent agents you create.


  1. Start by configuring an LLM and creating an agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
from superagent.client import Superagent

client = Superagent(
    base_url="https://api.beta.superagent.sh",
    token=os.environ["SUPERAGENT_API_KEY"]
)

llm = client.llm.create(request={
    "provider": "OPENAI",
    "apiKey": os.environ["OPENAI_API_KEY"]
})

agent = client.agent.create(request={
    "name": "Earnings Assistant",
    "description": "An Assistant that's an expert at analyzing earnings reports",
    "avatar": "",
    "isActive": True,
    "llmModel": "GPT_3_5_TURBO_16K_0613",
    "initialMessage": "Hi there, how can I help you?",
    "prompt": "You are an expert at analyzing earnings reports."
})

client.agent.add_llm(agent_id=agent.data.id, llm_id=llm.data.id)
  1. Now let's create a datasource by uploading Teslas Q3 2023 Earnings report to Superagent. Note that the description key tells the Assistant when this datasource should be used. Be vary of what you put in as your description as it might effect the Assistants behaviour.
1
2
3
4
5
6
7
8
9
10
11
12
datasource = client.datasource.create(request={
    "name": "Tesla Q3 2023",
    "description": "Useful for answering questions about Teslas Q3 2023 earnings report",
    "type": "PDF",
    "url": "https://digitalassets.tesla.com/tesla-contents/image/upload/IR/TSLA-Q3-2023-Update-3.pdf"
})

# Connect the datasource the the Agent
client.agent.add_datasource(
    agent_id=agent.data.id,
    datasource_id=datasource.data.id
)
  1. Now we can start chatting with the Assistant:
1
2
3
4
5
6
7
8
prediction = client.agent.invoke(
    agent_id=agent.data.id,
    input="What was Teslase revenue?",
    enable_streaming=False,
    session_id="my_session_id"
)

print(prediction.data.get("output"))

That's it! You can attach one or many datasources to your Assistants by following the same pattern. Be aware that there might be some rate limiting when adding many big datasources at once.

Full code

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
48
49
import os
from superagent.client import Superagent

client = Superagent(
    base_url="https://api.beta.superagent.sh",
    token=os.environ["SUPERAGENT_API_KEY"]
)

llm = client.llm.create(request={
    "provider": "OPENAI",
    "apiKey": os.environ["OPENAI_API_KEY"]
})

agent = client.agent.create(request={
    "name": "Earnings Assistant",
    "description": "An Assistant that's an expert at analyzing earnings reports",
    "avatar": "https://tesla.com/logo.png", # Replace with a real image
    "isActive": True,
    "llmModel": "GPT_3_5_TURBO_16K_0613",
    "initialMessage": "Hi there, how can I help you?",
    "prompt": "You are an expert at analyzing earnings reports.\nUse the earnings reports provided to answer any questions."
})

datasource = client.datasource.create(request={
    "name": "Tesla Q3 2023",
    "description": "Useful for answering questions about Teslas Q3 2023 earnings report",
    "type": "PDF",
    "url": "https://digitalassets.tesla.com/tesla-contents/image/upload/IR/TSLA-Q3-2023-Update-3.pdf"
})

# Connect the datasource and llm to the Agent
client.agent.add_datasource(
    agent_id=agent.data.id,
    datasource_id=datasource.data.id
)
client.agent.add_llm(agent_id=agent.data.id, llm_id=llm.data.id)

# Invoke the Assistant
prediction = client.agent.invoke(
    agent_id=agent.data.id,
    input="What was Teslase revenue?",
    enable_streaming=False,
    session_id="my_session_id"
)

print(prediction.data.get("output"))

# Teslas revenue was 24 Billing USD according to the earnings report.

Replit template

We've created a Replit template for this which you can run here.