Module for using MNN API
Project description
MNNAI
This repository contains an example of how to use the mnnai library.
Prerequisites
- Python 3.x
- MNNAI library installed. You can install it using pip:
pip install mnnai
Usage
Non-Streaming Chat
from mnnai import MNN
client = MNN(
key='MNN API KEY' # This is the default and can be omitted
)
chat_completion = client.chat.create(
messages=[
{
"role": "user",
"content": "What's the weather like in New York?",
}
],
model="gpt-4o-mini",
web_search=True # Internet search
)
print(chat_completion.choices[0].message.content)
Streaming Chat
stream = client.chat.create(
messages=[
{
"role": "user",
"content": "Will the neural networks capture the world?",
}
],
model="gpt-4o-mini",
stream=True
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
Image Generation
import base64
import os
response = client.images.create(
prompt="Draw a cute red panda",
model='dall-e-3'
)
image_base64 = response.data[0].url
os.makedirs('images', exist_ok=True)
for i, image_base64 in enumerate(image_base64):
image_data = base64.b64decode(image_base64)
with open(f'images/image_{i}.png', 'wb') as f:
f.write(image_data)
print("Images have been successfully downloaded!")
Async usage
Non-Streaming Chat
import asyncio
async def main():
chat_completion = await client.chat.async_create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-4o-mini",
)
print(chat_completion.choices[0].message.content)
asyncio.run(main())
Streaming Chat
import asyncio
async def main():
stream = await client.chat.async_create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say this is a test"}],
stream=True,
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
asyncio.run(main())
Image Generation
import asyncio
import base64
import os
async def main():
response = await client.images.async_create(
prompt="Draw a cute red panda",
model='dall-e-3',
n=4,
enhance=True
)
image_base64 = response.data[0].url
os.makedirs('images', exist_ok=True)
for i, image_base64 in enumerate(image_base64):
image_data = base64.b64decode(image_base64)
with open(f'images/image_{i}.png', 'wb') as f:
f.write(image_data)
print("Images have been successfully downloaded!")
asyncio.run(main())
Vision
With an image URL:
prompt = "What is in this image?"
img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Red_Panda_%2825193861686%29.jpg/1600px-Red_Panda_%2825193861686%29.jpg"
response = client.chat.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": img_url
}
},
]
}
],
)
With the image as a base64 encoded string:
import base64
image_path = "image.png"
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
base64_image = encode_image(image_path)
prompt = "What is in this image?"
response = client.chat.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
},
]
}
],
)
Auxiliary functions
Get models
print(client.GetModels())
Configuring the client
from mnnai import MNN
client = MNN(
key='MNN API KEY',
max_retries=2, # Number of retries in case of failure
timeout=60, # Maximum amount of time the request will be processed
debug=True # Whether the application needs to be debugged
)
License
This project is licensed under the MIT License. See the LICENSE file for details.
Discord
Project details
Release history Release notifications | RSS feed
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 mnnai-5.3.0.tar.gz.
File metadata
- Download URL: mnnai-5.3.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46087104a2c3cef39d0b18876ec6c9b3770f1aae78dac3e2b5155c29750317ec
|
|
| MD5 |
a1c2a6e8876736c702e9b0026e26a9ef
|
|
| BLAKE2b-256 |
9900e56707026b531d0d1cf571c42df9dabd98bc187170a50da8fd0f7d538e8d
|
File details
Details for the file mnnai-5.3.0-py3-none-any.whl.
File metadata
- Download URL: mnnai-5.3.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e14328c6f89cc842fc7d44bdf4ea6689ba94353e08396b0757abd42414d23b5
|
|
| MD5 |
cf1dc16089014f1d9381e4ce6bfe144d
|
|
| BLAKE2b-256 |
71efe0a2f831ea788395c0741b49faea24714e2d656d62b2ff2efa1aadcbec31
|