OpenAI Assistant With Retriever Tool

Cobus Greyling
6 min readNov 9, 2023

--

Some Background

What Can Assistants Do?

Retrieval Tool

Source
pip install openai

import os
import openai
import requests
import json
from openai import OpenAI

#Pass the retrieval in the tools parameter of the Assistant to enable Retrieval

api_key = "sk-rxolBSN8pMbAaQ7DUsxlT3BlbkFsfsfsfsfswrewrgwwrAfKazZs"

client = OpenAI(api_key=api_key)
assistant = client.beta.assistants.create(
name="General Knowledge Bot",
instructions="You are a customer support chatbot. Use your knowledge base to best respond to customer queries.",
model="gpt-4-1106-preview",
tools=[{"type": "retrieval"}]
)


#File upload via Colab Notebook
from google.colab import files
uploaded = files.upload()

for name, data in uploaded.items():
with open(name, 'wb') as file:
file.write(data)
print ('saved file', name)


#Pass the retrieval in the tools parameter of the Assistant to enable Retrieval
#Accessing the uploaded
file = client.files.create(
file=open("/content/Rugby_World_Cup.txt", "rb"),
purpose='assistants'
)

api_key = "sk-rxolBSN8pMbAaQ7DUsxlT3BlbkFsfsfsfsfswrewrgwwrAfKazZs"

client = OpenAI(api_key=api_key)
assistant = client.beta.assistants.create(
name="General Knowledge Bot",
instructions="You answer general knowledge questions as accureately as possible.",
model="gpt-4-1106-preview",
tools=[{"type": "retrieval"}]
)


##Files can also be added to a Message in a Thread. These files are only accessible within this specific thread.
##After having uploaded a file, you can pass the ID of this File when creating the Message.
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Who won the 2023 rugby world cup?",
file_ids=[file.id]
)

run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="You answer general knowledge questions as accureately as possible."
)

run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)

messages = client.beta.threads.messages.list(
thread_id=thread.id
)


print (messages)
"SyncCursorPage"[
"ThreadMessage"
]"(data="[
"ThreadMessage(id=""msg_nQqma6fUFnx9WhT5ilA11SxU",
"assistant_id=""asst_Z2FlbTYtCGetYhjDn7nroXEg",
"content="[
"MessageContentText(text=Text(annotations="[

],
"value=""South Africa, known as the Springboks, \n\n
won the 2023 Rugby World Cup by defeating New Zealand\n\n
in the final."")",
"type=""text"")"
],
created_at=1699450387,
"file_ids="[

],
"metadata="{

},
"object=""thread.message",
"role=""assistant",
"run_id=""run_hiQaWXIS9gIdWEV86vC3h9zE",
"thread_id=""thread_s0b6JzLU2o2uXZsw3b21LVcn"")",
"ThreadMessage(id=""msg_2zeWwRIbBiZzMp7Q1YnnVgjN",
"assistant_id=None",
"content="[
"MessageContentText(text=Text(annotations="[

],
"value=""Who won the 2023 rugby world cup? The file I uploaded can\n\n
help you with that."")",
"type=""text"")"
],
created_at=1699450378,
"file_ids="[
"file-e0GVGxhtPOOVuRdWDsd2Z8Jo"
],
"metadata="{

},
"object=""thread.message",
"role=""user",
"run_id=None",
"thread_id=""thread_s0b6JzLU2o2uXZsw3b21LVcn"")"
],
"object=""list",
"first_id=""msg_nQqma6fUFnx9WhT5ilA11SxU",
"last_id=""msg_2zeWwRIbBiZzMp7Q1YnnVgjN",
"has_more=False)"
"SyncCursorPage"[
"ThreadMessage"
]"(data="[
"ThreadMessage(id=""msg_ERivEkNDEb4s3UpDb6PRYQix",
"assistant_id=""asst_ouXMdIOna0blYE7Zm7kRKtp1",
"content="[
"MessageContentText(text=Text(annotations="[

],
"value=""As of my last update in April 2023, I do not have access \n\n
to real-time data, including current events or sports results.\n\n
To find out the latest information on the winner of the\n\n
2023 Rugby World Cup, I would recommend checking the latest\n\n
news on sports news websites, official tournament information,\n\n
or using a search engine for the most recent updates.\n\n
If you have access to these resources, they can provide you\n\n
with the answer you're looking for."")",
"type=""text"")"
],
created_at=1699450734,
"file_ids="[

],
"metadata="{

},
"object=""thread.message",
"role=""assistant",
"run_id=""run_3NhubSHCarv5593E7IhlsvY9",
"thread_id=""thread_logOF1PWWygNtZauY96LSeZR"")",
"ThreadMessage(id=""msg_HAW7HJIpTsPk762wDIlezYRb",
"assistant_id=None",
"content="[
"MessageContentText(text=Text(annotations="[

],
"value=""Who won the 2023 rugby world cup?"")",
"type=""text"")"
]
LinkedIn

--

--

Cobus Greyling
Cobus Greyling

Written by Cobus Greyling

I’m passionate about exploring the intersection of AI & language. www.cobusgreyling.com

Responses (4)