Skip to main content

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": "Say this is a test",
        }
    ],
    model="gpt-4o-mini"
)
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'
    )

    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())

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

https://discord.gg/Ku2haNjFvj

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

mnnai-5.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mnnai-5.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file mnnai-5.0.0.tar.gz.

File metadata

  • Download URL: mnnai-5.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for mnnai-5.0.0.tar.gz
Algorithm Hash digest
SHA256 877a08c22202eb94299f480e0a1a290c8e46d762e7f4fdcd79f8fd464870fc10
MD5 e19299989dce19f126ac17867c4e6582
BLAKE2b-256 d15761f1f42f9d9d39907a7489acdf73f4150df493e6f1ace7d4a20dfd38ee5a

See more details on using hashes here.

File details

Details for the file mnnai-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: mnnai-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for mnnai-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9e7288bea735ad09ad7ad90b8d7ae84121da63dbe62c1a762cebf356d6e6d21
MD5 ee49207f8d53207a6df93967d132ea78
BLAKE2b-256 5f71355e5127c1251daa4821bddccfbda7e5c075e823dd3fa0a89ecf47eddefd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page