Skip to main content

client for AICosmos platform

Project description

Client for AICosmos

This package implements the client for AICosmos. Before using this package, please make sure that you have a valid account for AICosmos.

Login & Create Session

By using this client, you can chat with our backend in "base" mode. To login, you will need the server's address and your API key. Our conversation system is based on sessions, you can either start a new session, or continue with an existing one.

from aicosmos_client.client import AICosmosClient 

# Login
client = AICosmosClient(
    base_url="https://aicosmos.ai/api", api_key="xxx"
)

# Create a new session
try:
    new_session_id = client.create_session()
except Exception as e:
    print(f"Error creating new session: {e}")
    exit(0)

# Check all existing sessions
try:
    my_sessions = client.get_my_sessions()
except Exception as e:
    print(f"Error getting my sessions: {e}")
    exit(0)
# [{"session_id", "title"}, ...]
print(my_sessions)

Chatting

Our framework is different from "chat completions", where you give an llm the conversation history. Instead, your conversation history, along with other tool execution results, are stored in our database. This gives you a clean and simple interface to use, without worrying about constructing complicated contexts. The following code starts a new conversation in "base" mode. We support both stream and non-stream responses.

Non-Stream Response

In this case, you only need to submit your prompt. The whole conversation history is returned.

# prompt = "Hello"
try:
    conversation_history = client.chat(session_id, prompt)
except Exception as e:
    print(f"Error chatting: {e}")
    exit(0)
print(conversation_history)

Stream Response

In this scenario, our server only returns the diff between current history and old history. There are two types of diffs:

  • added: a new item is added into conversation history
  • updated: certain fields of existing history has changed

The diffs are very intuitive. You can track the conversation history dinamically using them:

# get existing conversation history
tracked_history = client.get_session_history(session)

for diff in client.chat_stream(
    session_id,
    prompt,
):
    # add new items
    for add in diff["added"]:
        index = add["index"]
        item = add["item"]
        tracked_history.append(item)
    # update existing items
    for update in diff["updated"]:
        index = update["index"]
        field = update["field"]
        # new_suffix is a string
        new_suffix = update["new_suffix"]
        if field in tracked_history[index].keys():
            tracked_history[index][field] += new_suffix
        else:
            tracked_history[index] = new_suffix

# now `tracked_history` is up to date!

Other Modes

Apart from "base" mode, we also support "code" mode and "lean" mode. In these modes, we provide you with a web code editor, which you can open and interact with your web browser.

try:
    conversation_history = client.chat(
        new_session_id, "De Morgan's law. Start now.", mode="lean"
    )
except Exception as e:
    print(f"Error chatting: {e}")
    exit(0)
print(conversation_history)

# open this link with your web browser (e.g. Edge, Chrome)
url = client.get_browser_url(new_session_id)
print(url)

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

aicosmos_client-0.0.15.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

aicosmos_client-0.0.15-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file aicosmos_client-0.0.15.tar.gz.

File metadata

  • Download URL: aicosmos_client-0.0.15.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for aicosmos_client-0.0.15.tar.gz
Algorithm Hash digest
SHA256 86a2e84e3df79710ba83a736f15e8d5bac9eebdf5c0416ba0184aa566f86efc3
MD5 4ba136ad6f436504960b4193d920102c
BLAKE2b-256 9a7ca4d86961d877da495b4cae5e9fcb1023eec3187fe08c01ab50c85427f0c9

See more details on using hashes here.

File details

Details for the file aicosmos_client-0.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for aicosmos_client-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 58878c29d44c4fc5dd3437515c61550d6e3cf6e1bfbdfd640c49198a19cc226f
MD5 c7d7e2c3b4ac5c9695a22a9af681885e
BLAKE2b-256 37163e863e73be884bb92d168a5225b21bffa7f34e67b8e494d26b2683b7108f

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