Python library which wraps OpenAI Functions and makes them easier to use
Project description
AgentAI: OpenAI Functions + Python Functions
It is designed to make it easy to use OpenAI models e.g. GPT3.5-Turbo and GPT4 with existing Python functions by adding a simple decorator.
interact with databases, and handle structured data types.
AgentAI is a simple Python library with these ethos:
- Let developers write code!
- Do not invent a new syntax!
- Make it easy to integrate with existing projects!
- Make it easy to extend!
- Have fun and use exclamations!
Unlike some libraries, AgentAI does NOT require you to learn a new syntax. No chains!
Instead, it empowers you to add OpenAI functions using Python decorators and then call them directly from your code. This makes it easy to integrate AgentAI with your existing projects.
Features
- API Calls: Use AgentAI to decorate your Python functions and make them magical!
- SQL Database Interaction: Seamlessly extract and utilize data from SQL databases.
- Function Execution: Generate and execute function calls based on conversation context.
- Conversation Management: Effectively manage and track the state of conversations. Easily define your own functions which can use messages, functions, and conversation history.
Installation
Install AgentAI using pip:
pip install agentai
Usage
Getting Started: Asking User for Missing Inputs till all inputs are available
- Import required classes and functions
from agentai.api import chat_complete
from agentai.conversation import Conversation
from agentai.function_parser import tool
- Define a function with
@tool
decorator
from enum import Enum
class TemperatureUnit(Enum):
celsius = "celsius"
fahrenheit = "fahrenheit"
@tool
def get_current_weather(location: str, format: TemperatureUnit) -> str:
"""
Get the current weather
Args:
location (str): The city and state, e.g. San Francisco, CA
format (str): The temperature unit to use. Infer this from the users location.
Returns:
str: The current weather
"""
# Your function implementation goes here.
return ""
Note that agentai
automatically parses the Python Enum type (TemperatureUnit) and passes it to the model as a JSONSchema Enum. This saves you time in writing boilerplate JSONSchema which is required by OpenAI API.
- Create a Conversation object and add messages
conversation = Conversation()
conversation.add_message("user", "what is the weather like today?")
- Use the
chat_complete
function to get a response from the model
registered_functions = [get_current_weather]
functions = [json.loads(func.json_info) for func in registered_functions]
chat_response = chat_complete(conversation.conversation_history, functions=functions)
Output:
{'role': 'assistant',
'content': 'In which city would you like to know the current weather?'}
- Add user response to conversation and call
chat_complete
again
Once the user provides the required information, the model can generate the function arguments:
conversation.add_message("user", "I'm in Bengaluru, India")
chat_response = chat_complete(conversation.conversation_history, functions=functions, model=GPT_MODEL)
chat_response.json()["choices"][0]["message"]["function_call"]["arguments"]
Output:
{'location': 'Bengaluru, India', 'format': 'celsius'}
Ramping up Usage: Implement function execution logic (e.g., a database query)
- Define a function with
@tool
decorator
@tool
def ask_database(query: str) -> List[Tuple[str, str]]:
"""
Use this function to answer user questions about music. Input should be a fully formed SQL query.
Args:
query (str): SQL query extracting info to answer the user's question.
SQL should be written using this database schema: <database_schema_string>
IMPORTANT: Please return a fixed SQL in PLAIN TEXT.
Your response should consist of ONLY the SQL query.
"""
try:
results = conn.execute(query).fetchall()
return results
except Exception as e:
raise Exception(f"SQL error: {e}")
- Registering the function and using it
agentai_functions = [json.loads(func.json_info) for func in [ask_database]]
from agentai.api import chat_complete_execute_fn
agent_system_message = """You are ChinookGPT, a helpful assistant who gets answers to user questions from the Chinook Music Database.
Provide as many details as possible to your users
Begin!"""
sql_conversation = Conversation()
sql_conversation.add_message(role="system", content=agent_system_message)
sql_conversation.add_message("user", "Hi, who are the top 5 artists by number of tracks")
assistant_message = chat_complete_execute_fn(
conversation=sql_conversation, functions=agentai_functions, model=GPT_MODEL, callable_function=ask_database
)
sql_conversation.display_conversation(detailed=True)
Output:
system: You are ChinookGPT, a helpful assistant who gets answers to user questions from the Chinook Music Database.
Provide as many details as possible to your users
Begin!
user: Hi, who are the top 5 artists by number of tracks
function: [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]
assistant: The top 5 artists by number of tracks are:
1. Iron Maiden - 213 tracks
2. U2 - 135 tracks
3. Led Zeppelin - 114 tracks
4. Metallica - 112 tracks
5. Lost - 92 tracks
Detailed Examples
Check out our detailed notebooks with examples where we demonstrate how to integrate AgentAI with a chatbot to create a powerful conversational assistant that can answer questions using a SQLite database.
Contributing
We welcome contributions! Please see our contributing guidelines for more details.
Support
If you encounter any issues or require further assistance, please raise an issue on our GitHub repository.
We hope you enjoy using AgentAI and find it helpful in powering up your AI models. Happy coding!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file agentai-0.0.2.tar.gz
.
File metadata
- Download URL: agentai-0.0.2.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f15e7dc51eba33d27cd40954dc4faea03c3d76defd146bbc842393f9814c8aad |
|
MD5 | c9ae1be9e7466ec56415341054eac851 |
|
BLAKE2b-256 | 737d36fc4d675dc4ee635324d922363b743fd6810749aea33e3ac9cb545fbdd4 |
File details
Details for the file agentai-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: agentai-0.0.2-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 774e643fdc8a800e6f196a49f012cef8cc08f40ed243c7ee705fe6ac6c660c4b |
|
MD5 | d4eca70b97915b58f1f3bf82b5de0d91 |
|
BLAKE2b-256 | e3b572a0b2e8e29195c4d5e74b4094b29f2e2d7c399c17db00e43f565e3a5b20 |