Skip to main content

Keywords AI SDK allows you to interact with the Keywords AI API smoothly

Project description

Keywords AI SDK

Keywords AI Python SDK allows you to easily interact with the Keywords AI API.

Get started

Get started with Keywords AI in minutes

Installation

Users

Poetry

poetry add keywordsai

Pip

pip install keywordsai

Developers and Contributers

You can install the current directory as a python package via this command

poetry install

or

pip install . -e

Environment Variables

touch .env

Inside the .env file, you can configure the constants througout the library

DEBUG # Default is "False", set to "True" to enable debug mode for more verbose output
KEYWORDSAI_BASE_URL # Default is "https://api.keywordsai.co/api"
KEYWORDSAI_API_KEY # Your Keywords AI API Key

Change values during runtime (not recommended)

import keywordsai.keywordsai_config as config
config.KEYWORDSAI_BASE_URL = "some_url"

Usage

Proxy

With Keywords AI as a proxy, observability comes out of box.

from openai import OpenAI
import os
client = OpenAI(
    api_key=os.getenv("KEYWORDSAI_API_KEY"),
    base_url=os.getenv("KEYWORDSAI_BASE_URL")
)

# Use the client to make requests as you would with the OpenAI SDK

Wrapper (Beta)

Wrap around the OpenAI completion function to automatically log the request and response

Synchronous:

from keywordsai import KeywordsAI
from openai import OpenAI
client = OpenAI()
def test_generation():
    kai = KeywordsAI()
    try:
        wrapped_creation = kai.logging_wrapper(client.chat.completions.create)
        response = wrapped_creation(
            model=test_model,
            messages=test_messages,
            stream=False,
            extra_body={"mock_response": test_mock_response},
        )
        assert isinstance(response, ChatCompletion)
    except Exception as e:
        assert False, e


if __name__ == "__main__":
    generator = test_generation()

Asynchronous:

import sys
sys.path.append(".")
from keywordsai.core import KeywordsAI, AsyncGenerator
from openai import AsyncOpenAI
from openai.types.chat.chat_completion import ChatCompletion

client = AsyncOpenAI()

async def test_stream_generation():
    kai = KeywordsAI()
    try:
        wrapped_creation = kai.async_logging_wrapper(client.chat.completions.create)
        # wrapped_creation = oai_client.chat.completions.create
        response = await wrapped_creation(
            model=test_model,
            messages=test_messages,
            stream=True,
        )
        assert isinstance(response, AsyncGenerator)
        return response
    except Exception as e:
        print(e)

async def test_generation():
    kai = KeywordsAI()
    try:
        wrapped_creation = kai.async_logging_wrapper(client.chat.completions.create, keywordsai_params={
            "customer_identifier": "sdk_customer",
        })
        response = await wrapped_creation(
            model=test_model,
            messages=test_messages,
            stream=False,

        )
        assert isinstance(response, ChatCompletion)
        return response
    except Exception as e:
        assert False, e

import asyncio

async def run_stream():
    response = await test_stream_generation()
    async for chunk in response:
        content = chunk.choices[0].delta.content
        if content:
            print(content, end="")
        pass

if __name__ == "__main__":
    # non streaming
    asyncio.run(test_generation())

    # streaming
    asyncio.run(run_stream())
    KeywordsAI.flush()


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

keywordsai_sdk-0.2.49.tar.gz (78.8 kB view details)

Uploaded Source

Built Distribution

keywordsai_sdk-0.2.49-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file keywordsai_sdk-0.2.49.tar.gz.

File metadata

  • Download URL: keywordsai_sdk-0.2.49.tar.gz
  • Upload date:
  • Size: 78.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.4 Darwin/24.1.0

File hashes

Hashes for keywordsai_sdk-0.2.49.tar.gz
Algorithm Hash digest
SHA256 6a70483b831fe71900848f4a02161162f5281e1912523e4a88431049296ca2e8
MD5 337329aeb1256f13c9c4e8f5baaf913e
BLAKE2b-256 eede5cc7655e9ccfb40064759ff4e76db783910f259287d67b3393239dc80a0b

See more details on using hashes here.

File details

Details for the file keywordsai_sdk-0.2.49-py3-none-any.whl.

File metadata

  • Download URL: keywordsai_sdk-0.2.49-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.4 Darwin/24.1.0

File hashes

Hashes for keywordsai_sdk-0.2.49-py3-none-any.whl
Algorithm Hash digest
SHA256 175c5a3e4a766c33f7f8d6c49ccfaa47488ff48af2632cdca915fb75020c5939
MD5 9410d56379b08eac20e637fa566cbed2
BLAKE2b-256 a6b5fc3df7aaec41e63f06fb93c2eeb3170cad0b4ac3db26611f20a06168a2b0

See more details on using hashes here.

Supported by

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