Skip to content

Add file to agent

Adding File

The add_file_to_agent() function allows you to upload a file to an agent in Blipper. This function requires the following parameters:

  • agent_id (type: str): The ID of the agent to which the file will be added, provided as a string.
  • filename (type: str): The name of the file to be uploaded, provided as a string.
  • file (type: file object): The file object representing the content of the file to be uploaded.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from blipper import Blipper

blip_obj = Blipper(blipper_api_key="lqqRYOOnSerIOP973hjskR", model="galileo", verbose=False)

file = open("my-restaurant-info.pdf", "rb")  # open the file with your business info

file_id: str = blip_obj.add_file_to_agent(    # upload the file
    agent_id="my_agent_id",
    filename=file.filename,
    file=file
)

file.close()    # close the file

In this example, the file is opened using the open() function with the mode "rb" (read binary). Then, the add_file_to_agent() function is called with the opened file passed as the file parameter. Finally, the file is closed using the close() method.

Response: The file_id variable will contain the ID of the uploaded file as a string.

More:

If you are using Python there is a shortly way to open a and close a file using with(), This way is generally considered more concise and safer as it automatically closes the file when it's done

1
2
3
4
5
6
with open("my-restaurant-info.pdf", "rb") as file:
    file_id: str = blip_obj.add_file_to_agent(
        agent_id="my_agent_id",
        filename=file.filename,
        file=file
    )

FastAPI example

If you are using FastAPI, you can upload the file using the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# main.py

import uvicorn
from fastapi import FastAPI, UploadFile
from blipper import Blipper

app = FastAPI()

blip_obj = Blipper(blipper_api_key="lqqRYOOnSerIOP973hjskR", model="galileo", verbose=False)

@app.post("/upload-file-to-agent")
async def upload_file_to_agent(file: UploadFile):
    file_id: str = blip_obj.add_file_to_agent("my_agent_id", file.filename, file.file)
    return { "file_id": file_id }

if __name__=="__main__":
    uvicorn.run('main:app', host='0.0.0.0', port=8080, reload=True)

And in the client side:

<form action="localhost:8080/upload-file-to-agent/" enctype="multipart/form-data" method="post">
    <input name="file" type="file" required="required">
    <input type="submit">
</form>