#How to make assistant read document before executing my function

1 messages · Page 1 of 1 (latest)

earnest rune
#

@pastel carbon I am using assistants with its tools being retrieval and a function. Would I use a prompt to tell it to read a document before running the function or is there a better way?

pastel carbonBOT
#

Hi @earnest rune. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use the Retrieval tool to augment your Assistant with knowledge from outside its model, such as documents provided by your users. Once a file is uploaded and passed to the Assistant, OpenAI will automatically chunk your documents, index and store the embeddings, and implement vector search to retrieve relevant content to answer user queries.

To enable Retrieval, you can pass the retrieval in the tools parameter of the Assistant. Here's an example:

assistant = client.beta.assistants.create(
 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"}]
)

The model then decides when to retrieve content based on the user Messages. The Assistants API automatically chooses between two retrieval techniques: it either passes the file content in the prompt for short documents, or performs a vector search for longer documents.

After the retrieval process, you can then execute your function. However, please note that the model does not "read" the document in the traditional sense, but rather retrieves relevant content based on the user's queries.

For more information, you can refer to the OpenAI documentation on Retrieval (https://platform.openai.com/docs/assistants/tools#knowledge-retrieval).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@earnest rune kapa.ai is still learning and improving, please let us know how it did by reacting below

earnest rune
#

@pastel carbon my tools contains retrieval and a function whihc i want to be executed after the reading of the document, how can i achieve this?

pastel carbonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To achieve this, you would typically structure your conversation with the Assistant in a way that it first retrieves the necessary information from the document using the Retrieval tool, and then executes the function based on the retrieved information.

Here's a simplified sequence of steps:

  1. Call the Assistant with a user query that prompts it to retrieve information from the document. The Assistant will use the Retrieval tool to fetch relevant content from the document.

  2. Once the Assistant has retrieved the necessary information, you can then call your function. This can be done by sending another message to the Assistant that includes the function call.

Here's an example of how you might define your Assistant with both Retrieval and a custom function:

assistant = client.beta.assistants.create(
 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"}, {"type": "function", "name": "your_function_name"}]
)

Please note that the model decides when to retrieve content and when to call a function based on the user messages and the instructions you provide. Therefore, the way you structure your conversation and instructions is crucial.

For more information, you can refer to the OpenAI documentation on Retrieval (https://platform.openai.com/docs/assistants/tools#knowledge-retrieval) and Function Calling (https://platform.openai.com/docs/guides/function-calling).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@earnest rune kapa.ai is still learning and improving, please let us know how it did by reacting below

earnest rune
#

@pastel carbon so should i add a message to thread asking it to read the script, then start the run?

pastel carbonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you can add a message to the thread instructing the assistant to read the document. This message will serve as a prompt for the assistant to use the Retrieval tool and fetch relevant information from the document.

Here's an example of how you might structure your conversation:

# Create a thread
thread = client.beta.threads.create()

# Add a message to the thread instructing the assistant to read the document
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Please read the document and provide the necessary information."
)

# Start the run
run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions="Please execute the function after reading the document."
)

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

Please note that the actual code may vary depending on your specific use case and the OpenAI API version you are using. For more detailed information, you can refer to the OpenAI documentation (https://platform.openai.com/docs/introduction).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@earnest rune kapa.ai is still learning and improving, please let us know how it did by reacting below

earnest rune
#

@pastel carbon how do i tell it to use the data from the document because it generates its own data, not related to the document

pastel carbonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To instruct the assistant to use data from the document, you need to structure your prompt or user message in a way that explicitly asks the assistant to refer to the document for its responses.

For example, if you have a document about Azure AI services and machine learning, you might structure your user message like this:

message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Please refer to the Azure AI services and machine learning document to answer the following question: ..."
)

This way, the assistant is explicitly instructed to use the information from the document in its response.

However, please note that the assistant's ability to accurately retrieve and use information from the document depends on the quality of the document and the relevance of the information to the user's query. If the document does not contain the information needed to answer the query, the assistant might generate its own response based on its pre-existing knowledge.

For more information on how to use the Retrieval tool and structure your prompts, you can refer to the OpenAI documentation (https://platform.openai.com/docs/guides/prompt-engineering).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: