# Ez OpenAI
My opinion of the `openai` Python library is best illustrated by the fact that if you
ask ChatGPT about it, it will usually hallucinate a more reasonable API. So, I wrote
this library, because if I had to manually poll for a tool update again I would
instigate the robot uprising myself.
## Installation
Run this somewhere:
```
pip install ez-openai
```
## Usage
### Basic usage
Using Ez OpenAI is (hopefully) straightforward, otherwise I've failed at the one thing
I've set out to make:
```python
from ez_openai import Assistant
# To use a previously-created assistant:
ass = Assistant.get("asst_someassistantid")
# To create a new one:
ass = Assistant.create(
name="Weatherperson",
instructions="You are a helpful weatherperson.",
)
# Create a conversation.
conversation = ass.conversation.create()
# Ask it something.
reply = conversation.ask("How are you today?")
print(reply.text)
"I'm great, thank you! How can I assist you today?"
# You can store the ID for later.
assistant_id = ass.id
# Delete it when you're done.
ass.delete()
```
### Function calling
No more wizardry, just plain Python functions:
```python
from ez_openai import Assistant, openai_function
from enum import Enum
@openai_function(descriptions={
"city": "The city to get the weather for.",
"unit": "The temperature unit , either `c` or `f`.",
})
def get_weather(city: str, unit: Enum("unit", ["c", "f"])):
"""Get the weather for a given city, and in the given unit."""
# ...do some magic here to get the weather...
print(f"I'm getting the weather for {city} woooooo")
return {"temperature": 26, "humidity": "60%"}
ass = Assistant.create(
name="Weatherperson",
instructions="You are a helpful weatherperson.",
functions=[get_weather]
)
# Or, if you already have one, you can fetch it (but still
# need to specify the functions).
ass = Assistant.get("asst_O5ZAsccgOOtgjrcgHhUMloSA", functions=[get_weather])
conversation = ass.conversation.create()
# Similarly, you can store the ID to fetch later:
old_conversation = ass.conversation.get(old_conversation.id)
# The library will handle all the background function calls itself:
conversation.ask("Hi, what's the weather like in Thessaloniki and Athens right now?").text
> I'm getting the weather for Thessaloniki woooooo
> I'm getting the weather for Athens woooooo
> "The weather today in both Thessaloniki and Athens is quite similar, with a
temperature of 26°C and a humidity level at 60%. Enjoy a pleasant and comfortable
day!"
# You can give it additional instructions per-run:
conversation.ask(
"What's the weather like in Thessaloniki?",
additional_instructions="When asked about the weather, make up something random."
).text
> "Right now in Thessaloniki, it's a pleasant day with clear skies, a gentle breeze,
and temperatures around 22°C. It's a nice day to explore the city's waterfront!"
# It also supports images:
conversation.ask("What's in this image?", image_url="https://www.someimage.com/").text
# or:
conversation.ask("What's in this image?", image_file="file.jpg").text
```
Because assistants change (eg if you want to add some more functions), and it's tedious
to create new ones every time, there's a helper method that will update an assistant
with new functions/instructions:
```python
from ez_openai import Assistant
ass = Assistant.get_and_modify(
id="asst_someassistantid",
name="Weatherperson",
instructions="These are your new instructions.",
functions=[get_weather, some_new_function]
)
```
Note: The raw OpenAI message is returned in `EZMessage`'s `raw` field.
### Streaming
If you need to stream tokens, there's a streaming interface:
```python
stream = conversation.ask_stream("Say Hello World!")
for event in stream:
events.append(event)
message = stream.value
assert "Hello" in events[0].text
assert " World" in events[1].text
assert "!" in events[2].text
assert "Hello World!" in message.text
```
gg ez
Raw data
{
"_id": null,
"home_page": "https://github.com/skorokithakis/ez-openai/",
"name": "ez-openai",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Stavros Korokithakis",
"author_email": "hi@stavros.io",
"download_url": "https://files.pythonhosted.org/packages/8f/48/e8ba27408b98a5f136b0d1f72dc758a83c32e970b9ce8d93f5d6bf5bcfa5/ez_openai-0.0.7.tar.gz",
"platform": null,
"description": "# Ez OpenAI\n\nMy opinion of the `openai` Python library is best illustrated by the fact that if you\nask ChatGPT about it, it will usually hallucinate a more reasonable API. So, I wrote\nthis library, because if I had to manually poll for a tool update again I would\ninstigate the robot uprising myself.\n\n## Installation\n\nRun this somewhere:\n\n```\npip install ez-openai\n```\n\n## Usage\n\n### Basic usage\n\nUsing Ez OpenAI is (hopefully) straightforward, otherwise I've failed at the one thing\nI've set out to make:\n\n```python\nfrom ez_openai import Assistant\n\n# To use a previously-created assistant:\nass = Assistant.get(\"asst_someassistantid\")\n\n# To create a new one:\nass = Assistant.create(\n name=\"Weatherperson\",\n instructions=\"You are a helpful weatherperson.\",\n)\n\n# Create a conversation.\nconversation = ass.conversation.create()\n\n# Ask it something.\nreply = conversation.ask(\"How are you today?\")\n\nprint(reply.text)\n\"I'm great, thank you! How can I assist you today?\"\n\n# You can store the ID for later.\nassistant_id = ass.id\n\n# Delete it when you're done.\nass.delete()\n```\n\n### Function calling\n\nNo more wizardry, just plain Python functions:\n\n```python\nfrom ez_openai import Assistant, openai_function\nfrom enum import Enum\n\n@openai_function(descriptions={\n \"city\": \"The city to get the weather for.\",\n \"unit\": \"The temperature unit , either `c` or `f`.\",\n })\ndef get_weather(city: str, unit: Enum(\"unit\", [\"c\", \"f\"])):\n \"\"\"Get the weather for a given city, and in the given unit.\"\"\"\n # ...do some magic here to get the weather...\n print(f\"I'm getting the weather for {city} woooooo\")\n return {\"temperature\": 26, \"humidity\": \"60%\"}\n\n\nass = Assistant.create(\n name=\"Weatherperson\",\n instructions=\"You are a helpful weatherperson.\",\n functions=[get_weather]\n)\n\n# Or, if you already have one, you can fetch it (but still\n# need to specify the functions).\nass = Assistant.get(\"asst_O5ZAsccgOOtgjrcgHhUMloSA\", functions=[get_weather])\n\nconversation = ass.conversation.create()\n\n\n# Similarly, you can store the ID to fetch later:\nold_conversation = ass.conversation.get(old_conversation.id)\n\n\n# The library will handle all the background function calls itself:\nconversation.ask(\"Hi, what's the weather like in Thessaloniki and Athens right now?\").text\n> I'm getting the weather for Thessaloniki woooooo\n> I'm getting the weather for Athens woooooo\n> \"The weather today in both Thessaloniki and Athens is quite similar, with a\n temperature of 26\u00b0C and a humidity level at 60%. Enjoy a pleasant and comfortable\n day!\"\n\n\n# You can give it additional instructions per-run:\nconversation.ask(\n \"What's the weather like in Thessaloniki?\",\n additional_instructions=\"When asked about the weather, make up something random.\"\n).text\n> \"Right now in Thessaloniki, it's a pleasant day with clear skies, a gentle breeze,\n and temperatures around 22\u00b0C. It's a nice day to explore the city's waterfront!\"\n\n\n# It also supports images:\nconversation.ask(\"What's in this image?\", image_url=\"https://www.someimage.com/\").text\n\n# or:\nconversation.ask(\"What's in this image?\", image_file=\"file.jpg\").text\n```\n\nBecause assistants change (eg if you want to add some more functions), and it's tedious\nto create new ones every time, there's a helper method that will update an assistant\nwith new functions/instructions:\n\n```python\nfrom ez_openai import Assistant\n\nass = Assistant.get_and_modify(\n id=\"asst_someassistantid\",\n name=\"Weatherperson\",\n instructions=\"These are your new instructions.\",\n functions=[get_weather, some_new_function]\n)\n```\n\nNote: The raw OpenAI message is returned in `EZMessage`'s `raw` field.\n\n### Streaming\n\nIf you need to stream tokens, there's a streaming interface:\n\n```python\nstream = conversation.ask_stream(\"Say Hello World!\")\nfor event in stream:\n events.append(event)\nmessage = stream.value\n\nassert \"Hello\" in events[0].text\nassert \" World\" in events[1].text\nassert \"!\" in events[2].text\nassert \"Hello World!\" in message.text\n```\n\ngg ez\n\n",
"bugtrack_url": null,
"license": "AGPL-3.0-or-later",
"summary": "A more reasonable OpenAI API.",
"version": "0.0.7",
"project_urls": {
"Homepage": "https://github.com/skorokithakis/ez-openai/",
"Repository": "https://github.com/skorokithakis/ez-openai/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "305d830477770182dc7da12f54ef23c55d9cbdcec0c42b08b3a0ebbe74cc5807",
"md5": "ad7446868b5412067a8ff7116a2f3e29",
"sha256": "f419bbc4f38cf0a324da10d67f7e5d78d9ed1814cc3f9de3f883750215e218b4"
},
"downloads": -1,
"filename": "ez_openai-0.0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ad7446868b5412067a8ff7116a2f3e29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.8",
"size": 18334,
"upload_time": "2024-10-15T09:25:08",
"upload_time_iso_8601": "2024-10-15T09:25:08.695073Z",
"url": "https://files.pythonhosted.org/packages/30/5d/830477770182dc7da12f54ef23c55d9cbdcec0c42b08b3a0ebbe74cc5807/ez_openai-0.0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8f48e8ba27408b98a5f136b0d1f72dc758a83c32e970b9ce8d93f5d6bf5bcfa5",
"md5": "5cf23205eb9894a84ca2a0995622e212",
"sha256": "89a4df7ce4e50d664c60057f85ac866a8bcc015d40950bfda74c0944966163b6"
},
"downloads": -1,
"filename": "ez_openai-0.0.7.tar.gz",
"has_sig": false,
"md5_digest": "5cf23205eb9894a84ca2a0995622e212",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.8",
"size": 17561,
"upload_time": "2024-10-15T09:25:09",
"upload_time_iso_8601": "2024-10-15T09:25:09.867375Z",
"url": "https://files.pythonhosted.org/packages/8f/48/e8ba27408b98a5f136b0d1f72dc758a83c32e970b9ce8d93f5d6bf5bcfa5/ez_openai-0.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-15 09:25:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "skorokithakis",
"github_project": "ez-openai",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ez-openai"
}