Publishing My First Python Library
November, 2024
I have been programming in python for roughly two years, with many of my projects published on GitHub (I have 27s repos written in Python) but for the first time this week I started working on a python library.
In many of my small personal projects, I often experiment with different instances of Claude & ChatGPT. Every time I do this, I often write up the exact same basic wrapper class that smoothens communication with the OpenAI or Anthropic API. Instead of having multiple API calls with contexts associated with each, I have a few ChatBots.
I noticed that I often rewrite the exact same class with minor differences from API to API, so I made a python library. I got tired of rewriting the same class, so now when I want a ChatGPT instance it's very simple.
I pip install my library:
pip install api-chatbots
I set my OpenAI api key in my .env
file:
OPENAI_API_KEY='my-api-key'
Then I can import my library and use the chatbot.
from api_chatbots import ChatGPT
bot = ChatGPT(system_prompt="Respond as shakespeare.")
bot.add_user_message('Hello there!')
response = bot.respond()
print(response)
If I decide I would rather use Claude, I just need to provide my Anthropic API key, and switch the model. The rest of my code is unaffected.
Set my Anthropic key in .env
:
OPENAI_API_KEY='my-api-key'
ANTHROPIC_API_KEY='my-api-key'
Then switch the model:
from api_chatbots import Claude
bot = Claude(system_prompt="Respond as shakespeare.")
bot.add_user_message('Hello there!')
response = bot.respond()
print(response)
And all the other functions stay the same.
I wrote this library to save myself time when building short prototypes. I'm sure there are other libraries that inclu
de this exact feature, but I wanted something lightweight. All the code is on GitHub, here's the repo: https://github.com/Nicolas-Gatien/api-chatbots