Create a chatbot that can ingest files

Estimated reading: 8 minutes 201 views

This tutorial shows you how to build a chatbot that can read and answer questions about files you upload, such as meeting notes or job applications.

For example, you could upload a contract and ask, “What are the termination clauses in this agreement?” Or upload a resume and ask, “Does this candidate have experience with marketing analytics?”

The main focus of this tutorial is to show you how to provide files as input to a Robility flow, so your chatbot can use the content of those files in its responses.

Prerequisites

a. Create a Robility flow API key
b. Create an OpenAI API key

This tutorial uses OpenAI LLM. If you want to use a different provider, you need a valid credential for that provider.

Create a flow that accepts file input

To ingest files, your flow must have a File component attached to a component that receives input, such as a Prompt Template or Agent component.

The following steps modify the Basic Prompting template to accept file input:

1. In Robility flow, click New Flow, and then select the Basic Prompting template. 
2. In the Language Model component, enter your OpenAI API key.

If you want to use a different provider or model, edit the Model ProviderModel Name, and API Key fields accordingly.

3. To verify that your API key is valid, click Playground, and then ask the LLM a question. The LLM should respond according to the specifications in the Prompt Template component’s Template field. 
4. Exit the Playground and then modify the Prompt Template component to accept file input in addition to chat input. To do this, edit the Template field, and then replace the default prompt with the following text:

ChatInput:

{chat-input}

File:

{file}

Tips

You can use any string to name your template variables. These strings become the names of the fields (input ports) on the Prompt Template component.

For this tutorial, the variables are named after the components that connect to them: chat-input for the Chat Input component and file for the File component.

1. Add a File component to the flow, and then connect the Raw Content output port to the Prompt Template component’s file input port. To connect ports, click and drag from one port to the other.

You can add files directly to the File component to pre-load input before running the flow, or you can load files at runtime. The next section of this tutorial covers runtime file uploads.

At this point your flow has five components. The Chat Input and File components are connected to the Prompt Template component’s input ports. Then, the Prompt Template component’s output port is connected to the Language Model component’s input port. Finally, the Language Model component’s output port is connected to the Chat Output component, which returns the final response to the user.

Send requests to your flow from a Python application

This section of the tutorial demonstrates how you can send file input to a flow from an application.

To do this, your application must send a POST /run request to your Robility flow  server with the file you want to upload and a text prompt. The result includes the outcome of the flow run and the LLM’s response.

This example uses a local Robility flow  instance, and it asks the LLM to evaluate a sample resume. If you don’t have a resume on hand, you can download fake-resume.txt.

Points to note

For help with constructing file upload requests in Python, JavaScript, and curl, see the Robility flow File Upload Utility.

1. To construct the request, gather the following information:

a. ROBILITY FLOW _SERVER_ADDRESS: Your Robility flow server’s domain. The default value is 127.0.0.1:7860. You can get this value from the code snippets on your flow’s API access pane
b. FLOW_ID: Your flow’s UUID or custom endpoint name. You can get this value from the code snippets on your flow’s API access pane
c. FILE_COMPONENT_ID: The UUID of the File component in your flow, such as File-KZP68. To find the component ID, open your flow in Robility flow, click the File component, and then click Controls. The component ID is at the top of the Controls pane. 
d. CHAT_INPUT: The message you want to send to the Chat Input of your flow, such as Evaluate this resume for a job opening in my Marketing department.
e. FILE_NAME and FILE_PATH: The name and path to the local file that you want to send to your flow.
f. ROBILITY FLOW _API_KEY: A valid Robility flow API key.

2. Copy the following script into a Python file, and then replace the placeholders with the information you gathered in the previous step:

# Python example using requests

import requests

import json

# 1. Set the upload URL

url = “http://ROBILITY FLOW _SERVER_ADDRESS/api/v2/files/”

# 2. Prepare the file and payload

payload = {}

files = [

  (‘file’, (‘FILE_PATH’, open(‘FILE_NAME’, ‘rb’), ‘application/octet-stream’))

]

headers = {

  ‘Accept’: ‘application/json’,

  ‘x-api-key’: ‘ROBILITY FLOW _API_KEY’

}

# 3. Upload the file to Robility flow

response = requests.request(“POST”, url, headers=headers, data=payload, files=files)

print(response.text)

# 4. Get the uploaded file path from the response

uploaded_data = response.json()

uploaded_path = uploaded_data.get(‘path’)

# 5. Call the Robility flow  run endpoint with the uploaded file path

run_url = “http://ROBILITY FLOW _SERVER_ADDRESS/api/v1/run/FLOW_ID”

run_payload = {

    “input_value”: “CHAT_INPUT”,

    “output_type”: “chat”,

    “input_type”: “chat”,

    “tweaks”: {

        “FILE_COMPONENT_ID”: {

            “path”: uploaded_path

        }

    }

}

run_headers = {

    ‘Content-Type’: ‘application/json’,

    ‘Accept’: ‘application/json’,

    ‘x-api-key’: ‘ROBILITY FLOW _API_KEY’

}

run_response = requests.post(run_url, headers=run_headers, data=json.dumps(run_payload))

Robility flow _data = run_response.json()

# Output only the message

message = None

try:

    message = Robility flow _data[‘outputs’][0][‘outputs’][0][‘results’][‘message’][‘data’][‘text’]

except (KeyError, IndexError, TypeError):

    pass

print(message)

This script contains two requests.

The first request uploads a file, such as fake-resume.txt, to your Robility flow  server at the /v2/files endpoint. This request returns a file path that can be referenced in subsequent Robility flow  requests, such as 02791d46-812f-4988-ab1c-7c430214f8d5/fake-resume.txt

The second request sends a chat message to the Robility flow  flow at the /v1/run/ endpoint. The tweaks parameter includes the path to the uploaded file as the variable uploaded_path, and sends this file directly to the File component.

3. Save and run the script to send the requests and test the flow.

The initial output contains the JSON response object from the file upload endpoint, including the internal path where Robility flow stores the file. Then, the LLM retrieves the file and evaluates its content, in this case the suitability of the resume for a job position.

Next steps

To continue building on this tutorial, try these next steps.

Process multiple files loaded at runtime

To process multiple files in a single flow run, add a separate File component for each file you want to ingest. Then, modify your script to upload each file, retrieve each returned file path, and then pass a unique file path to each File component ID.

For example, you can modify tweaks to accept multiple File components. The following code is just an example; it isn’t working code:

## set multiple file paths

file_paths = {

    FILE_COMPONENT_1: uploaded_path_1,

    FILE_COMPONENT_2: uploaded_path_2

}

def chat_with_flow(input_message, file_paths):

    “””Compare the contents of these two files.”””

    run_url = f”{ROBILITY FLOW _SERVER_ADDRESS}/api/v1/run/{FLOW_ID}”

    # Prepare tweaks with both file paths

    tweaks = {}

    for component_id, file_path in file_paths.items():

        tweaks[component_id] = {“path”: file_path}

You can also use a Directory component to load all files in a directory or pass an archive file to the File component.

Upload external files at runtime

To upload files from another machine that isn’t your local environment, your Robility flow server must first be accessible over the internet. Then, authenticated users can upload files to your public Robility flow server’s /v2/files/ endpoint, as shown in the tutorial.

Preload files outside the chat session

You can use the File component to load files anywhere in a flow, not just in a chat session.

In the visual editor, you can preload files to the File component by selecting them from your local machine or Robility flow file management.

For example, you can preload an instructions file for a prompt template, or you can preload a vector store with documents that you want to query in a Retrieval Augmented Generation (RAG) flow.

For more information about the File component and other data loading components, see Data components.

Share this Doc

Create a chatbot that can ingest files

Or copy link

CONTENTS