Skip to main content

An unofficial async/sync client library for Straico API

Project description

Async Client Libary for the Straico API

A client side implementation of Straico API.

Installation

# install from PyPI
pip install aio-straico

Usage

Please see the official Straico API documentation https://documenter.getpostman.com/view/5900072/2s9YyzddrR

Basic Prompt Completion

from aio_straico import straico_client
from aio_straico.utils import cheapest_model 

def main():
    with straico_client(API_KEY="ko-11111111111111111111111111") as client:
        user_info = client.user()
        print(user_info)
        """
        {'coins': 100000.00,
          'first_name': 'User',
          'last_name': 'Name',
          'plan': 'License Tier 1'}
        """
        
        models = client.models()
        cheapest_chat_model = cheapest_model(models)
        print(cheapest_chat_model)
        """
        {'name': 'Google: Gemma 2 27B',  
         'model': 'google/gemma-2-27b-it',
         'word_limit': 3072,
         'pricing': {'coins': 0.4, 
                     'words': 100}}
        """
        
        reply = client.prompt_completion(cheapest_chat_model, "Hello there")
        print(reply["completion"]["choices"][0]["message"]["content"])
        """
        General Kenobi! 👋 

        What can I do for you today? 😊
        """
if __name__=="__main__":
    main()

Async Basic Prompt Completion

from aio_straico import aio_straico_client
from aio_straico.utils import cheapest_model 

async def main():
    async with aio_straico_client(API_KEY="ko-11111111111111111111111111") as client:
        user_info = await client.user()
        print(user_info)
        """
        {'coins': 100000.00,
          'first_name': 'User',
          'last_name': 'Name',
          'plan': 'License Tier 1'}
        """
        
        models = await client.models()
        cheapest_chat_model = cheapest_model(models)
        print(cheapest_chat_model)
        """
        {'name': 'Google: Gemma 2 27B',  
         'model': 'google/gemma-2-27b-it',
         'word_limit': 3072,
         'pricing': {'coins': 0.4, 
                     'words': 100}}
        """
        
        reply = await client.prompt_completion(cheapest_chat_model, "Hello there")
        print(reply["completion"]["choices"][0]["message"]["content"])
        """
        General Kenobi! 👋 

        What can I do for you today? 😊
        """
asyncio.run(main())

when API_KEY is not set in aio_straico_client, it will use the value from environment variable STRAICO_API_KEY. If no environment variable is found the program will raise an error.

You can also set the model name manually

reply = await client.prompt_completion("openai/gpt-4o-mini", "Hello there")
print(reply["completion"]["choices"][0]["message"]["content"])
"""
General Kenobi! 👋 

What can I do for you today? 😊
"""

Example Async Code

While the code below is async code, it can also be executed in a non-async mode by removing "await" and using the code with straico_client as shown in the "Basic Prompt Completion" section.

Add file attachment and Transcript

mp3_files = [*Path("test_data/audio/").glob("*.mp3")]
response = await client.prompt_completion(
    "openai/gpt-4o-mini",
    "summarize the main points",
    files=mp3_files,
    display_transcripts=True,
)

print("## Summary")
print(
    response["completions"]["openai/gpt-4o-mini"]["completion"]["choices"][0][
        "message"
    ]["content"]
)

print("## Transcript")
for transcript in response["transcripts"]:
    print("Name:", transcript["name"])
    print("Transcript:", transcript["text"])
    print()

"""
## Summary 
The . . .

## Transcript
Name:  . . .
Transcript: . . .
"""

Add Youtube URL and Transcript

youtube_url = "https://www.youtube.com/watch?v=zWPe_CUR4yU"

response = await client.prompt_completion(
    "openai/gpt-4o-mini",
    "summarize the main points",
    youtube_urls=youtube_url,
    display_transcripts=True,
)

print("## Summary")
print(
    response["completions"]["openai/gpt-4o-mini"]["completion"]["choices"][0][
        "message"
    ]["content"]
)

print("## Transcript")
for transcript in response["transcripts"]:
    print("Name:", transcript["name"])
    print("Transcript:", youtube_trasncript_to_plain_text(transcript["text"]))
    print()

"""
## Summary 
The . . .

## Transcript
Name:  . . .
Transcript: . . .
"""

Image Generation

Generate images and download zip file to local directory

model ="openai/dall-e-3"
directory = Path(".")
zip_file_path = await client.image_generation_as_zipfile(
    model=model,
    description="A cute cat",
    size=ImageSize.square,
    variations=4,
    destination_zip_path=directory,
)

Generate images and download image files to local directory

model ="openai/dall-e-3"
directory = Path(".")
image_paths = await client.image_generation_as_images(
    model=model,
    description="A cute cat",
    size=ImageSize.landscape,
    variations=4,
    destination_zip_path=directory,
)

RAG (Retrieval Augmented Generation)

Creating a RAG

Async Example
from aio_straico import aio_straico_client
from pathlib import Path

async def main():
    async with aio_straico_client() as client:
        # Create a RAG by providing a name, description, and files to index
        rag = await client.create_rag(
            "My Code RAG", 
            "RAG for my project files",
            Path("./my_project/utils.py"),
            Path("./my_project/main.py")
        )
        print(rag)  # Print RAG details

        # Alternatively, create a new RAG object
        rag_obj = await client.new_rag(
            "My Code RAG", 
            "RAG for my project files",
            Path("./my_project/utils.py"),
            Path("./my_project/main.py")
        )
        print(rag_obj.data)
Synchronous Example
from aio_straico import straico_client
from pathlib import Path

def main():
    with straico_client() as client:
        # Create a RAG by providing a name, description, and files to index
        rag = client.create_rag(
            "My Code RAG", 
            "RAG for my project files",
            Path("./my_project/utils.py"),
            Path("./my_project/main.py")
        )
        print(rag)  # Print RAG details

        # Alternatively, create a new RAG object
        rag_obj = client.new_rag(
            "My Code RAG", 
            "RAG for my project files",
            Path("./my_project/utils.py"),
            Path("./my_project/main.py")
        )
        print(rag_obj.data)

Deleting a RAG

Async Example
async def delete_rag():
    async with aio_straico_client() as client:
        # Get existing RAGs
        rags = await client.rags()
        rag_id = rags[0]['_id']  # Get ID of first RAG

        # Delete RAG by ID
        await client.rag_delete(rag_id)

        # Or delete RAG object directly
        rag_obj = await client.rag(rag_id)
        await rag_obj.delete()
Synchronous Example
def delete_rag():
    with straico_client() as client:
        # Get existing RAGs
        rags = client.rags()
        rag_id = rags[0]['_id']  # Get ID of first RAG

        # Delete RAG by ID
        client.rag_delete(rag_id)

        # Or delete RAG object directly
        rag_obj = client.rag(rag_id)
        rag_obj.delete()

RAG Prompt Completion

Async Example
async def rag_prompt():
    async with aio_straico_client() as client:
        # Get existing RAGs
        rags = await client.rags()
        rag_id = rags[0]['_id']  # Get ID of first RAG

        # Get available models
        models = await client.models()
        cheapest_model = cheapest_model(models)

        # Perform RAG prompt completion
        response = await client.rag_prompt_completion(
            rag_id, 
            cheapest_model, 
            "Explain the main functionality of my project"
        )
        print(response)

        # Alternatively, with RAG object
        rag_obj = await client.rag(rag_id)
        response = await rag_obj.prompt_completion(
            cheapest_model, 
            "Explain the main functionality of my project"
        )
        print(response)
Synchronous Example
def rag_prompt():
    with straico_client() as client:
        # Get existing RAGs
        rags = client.rags()
        rag_id = rags[0]['_id']  # Get ID of first RAG

        # Get available models
        models = client.models()
        cheapest_model = cheapest_model(models)

        # Perform RAG prompt completion
        response = client.rag_prompt_completion(
            rag_id, 
            cheapest_model, 
            "Explain the main functionality of my project"
        )
        print(response)

        # Alternatively, with RAG object
        rag_obj = client.rag(rag_id)
        response = rag_obj.prompt_completion(
            cheapest_model, 
            "Explain the main functionality of my project"
        )
        print(response)

Agents

Creating an Agent

Async Example
from aio_straico import aio_straico_client
from aio_straico.utils import cheapest_model

async def create_agent():
    async with aio_straico_client() as client:
        # Get available models
        models = await client.models()
        cheapest_chat_model = cheapest_model(models)

        # Create a new agent
        agent = await client.create_agent(
            "My Project Agent", 
            "An agent to help with my project",
            cheapest_chat_model,
            "You are a helpful assistant for my project.",
            ["Python", "Project"]
        )
        print(agent)  # Print agent details

        # Alternatively, create a new agent object
        agent_obj = await client.new_agent(
            "My Project Agent", 
            "An agent to help with my project",
            cheapest_chat_model,
            "You are a helpful assistant for my project.",
            ["Python", "Project"]
        )
        print(agent_obj.data)
Synchronous Example
from aio_straico import straico_client
from aio_straico.utils import cheapest_model

def create_agent():
    with straico_client() as client:
        # Get available models
        models = client.models()
        cheapest_chat_model = cheapest_model(models)

        # Create a new agent
        agent = client.create_agent(
            "My Project Agent", 
            "An agent to help with my project",
            cheapest_chat_model,
            "You are a helpful assistant for my project.",
            ["Python", "Project"]
        )
        print(agent)  # Print agent details

        # Alternatively, create a new agent object
        agent_obj = client.new_agent(
            "My Project Agent", 
            "An agent to help with my project",
            cheapest_chat_model,
            "You are a helpful assistant for my project.",
            ["Python", "Project"]
        )
        print(agent_obj.data)

Adding a RAG to an Agent

Async Example
async def add_rag_to_agent():
    async with aio_straico_client() as client:
        # Get existing RAGs and Agents
        rags = await client.rags()
        rag_id = rags[0]['_id']  # Get ID of first RAG

        agents = await client.agents()
        agent_id = agents[0]['_id']  # Get ID of first agent

        # Add RAG to agent by ID
        await client.agent_add_rag(agent_id, rag_id)

        # Or add RAG to agent object directly
        agent_obj = await client.agent(agent_id)
        await agent_obj.update(rag=rag_id)
Synchronous Example
def add_rag_to_agent():
    with straico_client() as client:
        # Get existing RAGs and Agents
        rags = client.rags()
        rag_id = rags[0]['_id']  # Get ID of first RAG

        agents = client.agents()
        agent_id = agents[0]['_id']  # Get ID of first agent

        # Add RAG to agent by ID
        client.agent_add_rag(agent_id, rag_id)

        # Or add RAG to agent object directly
        agent_obj = client.agent(agent_id)
        agent_obj.update(rag=rag_id)

Updating an Agent

Async Example
async def update_agent():
    async with aio_straico_client() as client:
        # Get an existing agent
        agents = await client.agents()
        agent_id = agents[0]['_id']

        # Update agent details
        await client.agent_update(
            agent_id, 
            name="Updated Agent Name",
            description="Updated description",
            system_prompt="You are an updated helpful assistant."
        )

        # Or update agent object directly
        agent_obj = await client.agent(agent_id)
        await agent_obj.update(
            name="Updated Agent Name",
            description="Updated description",
            system_prompt="You are an updated helpful assistant."
        )
Synchronous Example
def update_agent():
    with straico_client() as client:
        # Get an existing agent
        agents = client.agents()
        agent_id = agents[0]['_id']

        # Update agent details
        client.agent_update(
            agent_id, 
            name="Updated Agent Name",
            description="Updated description",
            system_prompt="You are an updated helpful assistant."
        )

        # Or update agent object directly
        agent_obj = client.agent(agent_id)
        agent_obj.update(
            name="Updated Agent Name",
            description="Updated description",
            system_prompt="You are an updated helpful assistant."
        )

Agent Prompt Completion

Async Example
async def agent_prompt():
    async with aio_straico_client() as client:
        # Get existing agents
        agents = await client.agents()
        agent_id = agents[0]['_id']  # Get ID of first agent

        # Perform agent prompt completion
        response = await client.agent_prompt_completion(
            agent_id, 
            "Explain the main functionality of my project"
        )
        print(response)

        # Alternatively, with agent object
        agent_obj = await client.agent(agent_id)
        response = await agent_obj.prompt_completion(
            "Explain the main functionality of my project"
        )
        print(response)
Synchronous Example
def agent_prompt():
    with straico_client() as client:
        # Get existing agents
        agents = client.agents()
        agent_id = agents[0]['_id']  # Get ID of first agent

        # Perform agent prompt completion
        response = client.agent_prompt_completion(
            agent_id, 
            "Explain the main functionality of my project"
        )
        print(response)

        # Alternatively, with agent object
        agent_obj = client.agent(agent_id)
        response = agent_obj.prompt_completion(
            "Explain the main functionality of my project"
        )
        print(response)

Deleting an Agent

Async Example
async def delete_agent():
    async with aio_straico_client() as client:
        # Get existing agents
        agents = await client.agents()
        agent_id = agents[0]['_id']  # Get ID of first agent

        # Delete agent by ID
        await client.agent_delete(agent_id)

        # Or delete agent object directly
        agent_obj = await client.agent(agent_id)
        await agent_obj.delete()
Synchronous Example
def delete_agent():
    with straico_client() as client:
        # Get existing agents
        agents = client.agents()
        agent_id = agents[0]['_id']  # Get ID of first agent

        # Delete agent by ID
        client.agent_delete(agent_id)

        # Or delete agent object directly
        agent_obj = client.agent(agent_id)
        agent_obj.delete()

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

aio_straico-0.1.6.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

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

aio_straico-0.1.6-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file aio_straico-0.1.6.tar.gz.

File metadata

  • Download URL: aio_straico-0.1.6.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for aio_straico-0.1.6.tar.gz
Algorithm Hash digest
SHA256 77cc2fad9147b2a6265ac69021361c3b97d1fa748257b50e46c7f6d7effc0efb
MD5 03b7a45e9c288f787e9f3b959093a1cb
BLAKE2b-256 ebb6ade1ddac78dd002af2e0221274ff3f5117a92f9d245d12940f8ffedbeb99

See more details on using hashes here.

File details

Details for the file aio_straico-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: aio_straico-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 27.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for aio_straico-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3d62f12c75b8877069ca5ed3985c631652ae8682178915021b31ae5d887645d2
MD5 bf8aefa9bd49f9e39928561fa4cab634
BLAKE2b-256 7e8f584d9547a4bddda340fc2890f122b57c879143933a33844b7df2b6537b5b

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