This repository contains a lightweight, open-source codebase designed to work with OpenAI's Assistant API, enabling the creation of custom Assistants and functions. This project is currently under active development, and while fully functional, the Assistants may require further modifications to better suit your needs.
To get started with this project, please follow the instructions below.
Clone the repository:
git clone https://github.com/Dataherald/Assistant
Navigate to the project directory:
cd Assistant
Install the required dependencies:
pip install -r requirements.txt
Create a .env file and set OPENAI_API_KEY = "your_api_key_here"
This repository allows you to create custom functions that can be integrated with an AI Assistant. Below is an example of how to set up a custom function and use it with the Assistant.
To create a custom function, you must create a new class that inherits from the Function
class.
Your class must have the following attributes:
name
: The name of the function.description
: A description of the function.parameters
: A list of Property
objects that define the parameters of the function. This is optional, and can be left blank if the function does not require any parameters.Each Property object must have the following attributes:
name
: The name of the parameter.description
: A description of the parameter.type
: The type of the parameter. This can be any type supported by OpenAI's API.required
: A boolean value indicating whether the parameter is required or not.Finally, you have to implement the function method, which will be called when the function is invoked. The function method must accept the same number of parameters as the number of parameters defined in the parameters
attribute.
Below is an example of a custom function that runs a SQL query on the Chinook database.
from function import Function, Property
class RunSQLQuery(Function):
def __init__(self):
super().__init__(
name="run_sql_query",
description="Run a SQL query on the Chinook database",
parameters=[
Property(
name="query",
description="The SQL query to run",
type="string",
required=True,
),
]
)
def function(self, query):
conn = sqlite3.connect('Chinook.sqlite')
cursor = conn.cursor()
cursor.execute(query)
results = cursor.fetchall()
conn.close()
return '\n'.join([str(result) for result in results])
To create an Assistant, you must create an object of the Assistant
class. The constructor of the class accepts the following parameters:
An example is provided below:
from assistant import AIAssistant
assistant = AIAssistant(
instruction="""
You are a SQL expert. User asks you questions about the Chinook database.
First obtain the schema of the database to check the tables and columns, then generate SQL queries to answer the questions.
""",
model="gpt-3.5-turbo-1106",
functions=[GetDBSchema(), RunSQLQuery()],
use_code_interpreter=True,
)
You can start chatting with the assistant by simply calling the chat() function as follows:
assistant.chat()
Using retireval tool is very simple, just enable the retrieval and upload your file, then pass it to the chat function as follows:
assistant = AIAssistant(
instruction=""" You are a helpful agent that helps user with their question about LLMs.""",
model="gpt-4-1106-preview",
use_retrieval=True,
)
file_id = assistant.upload_file("assistants_files/llama2_paper.pdf")
assistant.chat(file_ids=[file_id])
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
git checkout -b feature/AmazingFeature
.git commit -m 'Add some AmazingFeature'
.git push origin feature/AmazingFeature
.