A package to manage connections to AI model services such as OpenAI and Azure OpenAI.
Project description
codedharmony-ai-models
codedharmony-ai-models is a Python package that provides a unified interface for connecting to and interacting with various AI model services—such as OpenAI and Azure OpenAI—using prebuilt functions. By abstracting the connection and interaction logic into reusable functions, this package makes it easy to integrate advanced AI capabilities into your projects while minimizing boilerplate code and simplifying maintenance.
Overview
Instead of writing repetitive code to initialize API clients, manage credentials, and handle API responses for each service separately, codedharmony-ai-models offers dedicated functions that:
- Initialize and configure clients for both OpenAI and Azure OpenAI.
- Load sensitive configuration from environment variables (using a
.envfile). - Send chat or completion requests with consistent parameters.
- Return parsed responses for immediate use in your application.
Centralizing these responsibilities leads to:
- Convenience: Call a single function (e.g.,
create_chat_completion()) without worrying about setting up clients or managing credentials repeatedly. - Consistency: A uniform interface to both OpenAI and Azure OpenAI minimizes the learning curve.
- Maintainability: Updates (such as API version changes) need only be made in one place.
- Security: Using a
.envfile (and thepython-dotenvpackage) allows you to keep sensitive data out of your source code.
Installation
Install the package using pip (once published to PyPI):
pip install codedharmony-ai-models
Environment Setup
To keep your API keys and endpoints secure, create a .env file in the root of your project with the required variables. For example:
# .env file
# For OpenAI API (if used)
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_MODEL=your_model_name
# For Azure OpenAI Service
AZURE_OPENAI_API_KEY=your_azure_api_key_here
AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com
DEPLOYMENT_NAME=your_deployment_name_here
API_VERSION=api_version
Important:
- Add the .env file to your .gitignore to prevent accidentally committing sensitive information.
- The package automatically loads these variables by calling load_dotenv() (from the python-dotenv package) during initialization. Ensure that you have installed this dependency:
pip install python-dotenv
Usage Examples
Using the OpenAI Connector
The OpenAI connector provides functions to set up and call the OpenAI API easily. For example:
# File: example_openai.py
from codedharmony_ai_models.openai_connector import create_chat_completion
# Read a prompt from the user
prompt = input("Ask question for OpenAI: ")
# Define a system message for context
system_message = "You are a knowledgeable assistant specializing in artificial intelligence."
# Create a chat completion using the OpenAI API
response_text = create_chat_completion(
prompt,
system_message=system_message,
max_tokens=200, # Adjust maximum tokens as needed
temperature=0.8, # Control the creativity of the response
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=None # Optionally set stop sequences
)
print("OpenAI response:", response_text)
Using the Azure OpenAI Connector
The Azure OpenAI connector works similarly but is tailored for Azure’s deployment model. Ensure you have set the environment variables AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT before running:
# File: example_azure.py
from codedharmony_ai_models.azure_connector import create_azure_chat_completion
# Read a prompt from the user
prompt = input("Ask question for Azure OpenAI: ")
# Create a chat completion using Azure OpenAI Service
response_text = create_azure_chat_completion(
prompt,
max_tokens=200, # Customize as needed
temperature=0.8, # Adjust for more or less randomness
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=None # Optionally specify stop sequences
)
print("Azure OpenAI response:", response_text)
Why Use This Package?
Using codedharmony-ai-models offers several key advantages over manually integrating AI APIs:
Convenience:
The package centralizes the connection logic so that you simply call a function (e.g., create_chat_completion()) without having to set up API clients and manage credentials throughout your code.
Consistency:
Both OpenAI and Azure OpenAI are accessed through a similar interface. This uniformity reduces the learning curve and minimizes the chance for errors when switching between services.
Maintainability:
Updates to API versions or connection details only need to be made in one place (inside the connector modules), rather than scattered throughout your project. This decouples your business logic from low-level API details.
Flexibility:
The abstraction allows you to easily switch endpoints or even add support for additional AI model providers in the future without refactoring your entire codebase.
Simplified Error Handling:
By encapsulating API calls within dedicated functions, you can standardize error handling and logging, making it easier to debug issues compared to handling raw HTTP responses.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codedharmony_ai_models-0.1.3.tar.gz.
File metadata
- Download URL: codedharmony_ai_models-0.1.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
014d8d01e9e4c98ff3a90a4ae16d8dc6690987af13a53930bf1375bbe30f6f7c
|
|
| MD5 |
c3fc7bd21d8ed9cb2166334a9183f7fa
|
|
| BLAKE2b-256 |
2577424213acb2af6c80cc55380b7941d4bb321dd95dcfef61d3f14ce80a1e9f
|
File details
Details for the file codedharmony_ai_models-0.1.3-py3-none-any.whl.
File metadata
- Download URL: codedharmony_ai_models-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92840f3f25847d7c893426b75656917b7fb5f216d51b687929450ed1bbf989de
|
|
| MD5 |
9bba359c5e1ce06c9166d033c788f5ee
|
|
| BLAKE2b-256 |
f476e53113a2b087670c1d3af534d8a3957b951195ac658516720291d81417d5
|