Skip to main content

The official Python library for the nexusflowai API

Project description

NexusflowAI Python API library

PyPI version

Welcome to the NexusflowAI API by Nexusflow.ai!

pip install nexusflowai

This package is based on and extends from the OpenAI Python Library. Cheers to the OpenAI team for an amazing API library and SDK!

Usage

Completions

from nexusflowai import NexusflowAI


nf = NexusflowAI(api_key="<api key>")


response = nf.completions.create(
    model="nexus-tool-use-20240816",
    prompt="""Function:
def get_weather(city_name: str):
\"\"\"
\"\"\"


User Query: i am in berkeley.<human_end>Call:""",
    stop=["<bot_end>"],
    max_tokens=10,
)
print(response)

ChatCompletions with Tools

from nexusflowai import NexusflowAI


nf = NexusflowAI(api_key="<api key>")


response = nf.chat.completions.create(
    model="nexus-tool-use-20240816",
    messages=[
        {
            "role": "user",
            "content": "i am in berkeley.",
        },
    ],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city_name": {
                            "type": "string",
                            "description": "",
                        },
                    },
                    "required": ["city_name"],
                    "additionalProperties": False,
                }
            }
        }
    ],
)
print(response)

Multiturn ChatCompletions with Tools + Planning

For Multiturn Chat Completions with Planning, you must use the "nexusflowai_extras" message parameter returned with the chat completion in the previous response.

To do so, simply append the message output of the previous chat completion call to the conversation history.

Example:

from nexusflowai import NexusflowAI


nf = NexusflowAI(api_key="<api key>")

tools_list = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "returns True if weather is nice, False otherwise",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": [],
                "additionalProperties": False,
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "call_taxi",
            "description": "",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": [],
                "additionalProperties": False,
            }
        }
    }
]

# Setup Initial Message
messages_list = [
    {
        "role": "user",
        "content": "Get the weather, and then call a taxi.",
    },
]

response = nf.chat.completions.create(
    model="nexus-tool-use-20240816",
    messages=messages_list,
    tools=tools_list
)

print(response.model_dump_json(indent=4))
"""
Output contains `nexusflowai_extras` field in the chat.completion.choices.messages parameter.

output:
{
    "id": "44d060af-ae7c-4166-a3ab-2937231bb126",
    "choices": [
        {
            "finish_reason": "tool_calls",
            "index": 0,
            "message": {
                "content": null,
                "refusal": null,
                "role": "assistant",
                "tool_calls": [
                    {
                        "id": "call_de128c8884e24a0aba92ae88ea40852b",
                        "function": {
                            "arguments": "{}",
                            "name": "get_weather"
                        },
                        "type": "function",
                        "execution_result": null
                    },
                    {
                        "id": "call_e9cbc49b5bdd477da7c2db3deb0d7709",
                        "function": {
                            "arguments": "{}",
                            "name": "call_taxi"
                        },
                        "type": "function",
                        "execution_result": null
                    }
                ],
                "parsed": null,
                "nexusflowai_extras": "{\"original_plan\": \"get_weather(); call_taxi()\"}"
            }
        }
    ],
    "created": 1733280031,
    "model": "nexus-tool-use-20240816",
    "object": "chat.completion",
    "system_fingerprint": null,
    "usage": {
        "completion_tokens": 8,
        "prompt_tokens": 54,
        "total_tokens": 62,
        "latency": 0.35005688667297363,
        "time_to_first_token": null,
        "output_tokens_per_sec": null
    },
    "hints": null
}
"""

# Add previous assistant message with the `nexusflowai_extras` field to messages
messages_list.append(
    response.choices[0].message
)

response = nf.chat.completions.create(
    model="nexus-tool-use-20240816",
    messages=messages_list,
    tools=tools_list,
)

print(response.model_dump_json(indent=4))
"""
output:
{
    "id": "2bc46052-9951-475d-b0d6-6471207bdc90",
    "choices": [
        {
            "finish_reason": "tool_calls",
            "index": 0,
            "message": {
                "content": null,
                "refusal": null,
                "role": "assistant",
                "tool_calls": [
                    {
                        "id": "call_fc412fc4f399449a88a5cce69d0635ab",
                        "function": {
                            "arguments": "{}",
                            "name": "call_taxi"
                        },
                        "type": "function",
                        "execution_result": null
                    }
                ],
                "parsed": null,
                "nexusflowai_extras": "{\"original_plan\": \"call_taxi()\"}"
            }
        }
    ],
    "created": 1733279748,
    "model": "nexus-tool-use-20240816",
    "object": "chat.completion",
    "system_fingerprint": null,
    "usage": {
        "completion_tokens": 5,
        "prompt_tokens": 61,
        "total_tokens": 66,
        "latency": 0.24212288856506348,
        "time_to_first_token": null,
        "output_tokens_per_sec": null
    },
    "hints": null
}
"""

ChatCompletions with Structured Outputs

from typing import List, Dict, Tuple

from pydantic import BaseModel, Field

from nexusflowai import NexusflowAI


nf = NexusflowAI(api_key="<api key>")


class GasDistributionNetwork(BaseModel):
    networkID: str = Field(
        ...,
        description="The identifier for the gas distribution network.",
        title="Network ID",
    )
    pipelineValues: Dict[str, Tuple[int, int]] = Field(
        description="The mapping with key pipeline_1, pipeline_2, etc ... to tuple of (total length in kilometers, maximum amount of gas that can be distributed in cubic meters).",
        title="Pipeline Values",
    )
    maintenanceSchedules: List[str] = Field(
        ...,
        description="The schedule detailing when maintenance activities are to be performed.",
        title="Maintenance Schedule",
    )


response = nf.chat.completions.create(
    model="nexus-tool-use-20240816",
    messages=[
        {
            "role": "user",
            "content": """I am currently working on a project that involves mapping out a gas distribution network for a new residential area. The network is quite extensive and includes several pipelines that distribute natural gas to various sectors of the community. I need to create a JSON object that captures the essential details of this network. The information I have includes a unique identifier for the network, which is 'GDN-4521'. The total length of the pipeline_1 is 275 kilometers with a capacity 500,000 cubic meters. Pipeline 2 is 17 kilometers long and has a capacity of 12,000 cubic meters. Additionally, there is a detailed maintenance schedule, which includes quarterly inspections in January, April, July, and October.""",
        },
    ],
    response_format=GasDistributionNetwork,
)
print(response.raw_prompt)
print(response.choices[0].message.parsed)

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

nexusflowai-0.1.9.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

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

nexusflowai-0.1.9-py3-none-any.whl (53.5 kB view details)

Uploaded Python 3

File details

Details for the file nexusflowai-0.1.9.tar.gz.

File metadata

  • Download URL: nexusflowai-0.1.9.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for nexusflowai-0.1.9.tar.gz
Algorithm Hash digest
SHA256 96cfe1877a22022e654abaada545ddc4b893008ccf6b68e32f491551a36babf7
MD5 334a33f416f1f11e4026dbe8c12fb9a9
BLAKE2b-256 2fe47b0e101721afa01a6c3cd14e5bfabe48c03a0c884e44c6196b79d4b60279

See more details on using hashes here.

File details

Details for the file nexusflowai-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: nexusflowai-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 53.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for nexusflowai-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 e870ded2a16a975a8d7f56aee642ffbe2d543705aa1352496903d97b3434cf12
MD5 611f076528cd0c05ecbeb1ce8a39b1f2
BLAKE2b-256 5c8c8806561dab3f1de75be42c378ce9076255ff3f6346cfc168f1196d360bd0

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