Skip to main content

A Python client for the Interlify API

Project description

Interlify Client

A Python client for the Interlify API.

Installation

Install via pip:

pip install interlify

Usage:

from openai import OpenAI
from interlify import Interlify


client = OpenAI()

# Initialize the client
interlify = Interlify(
    api_key="YOUR_API_KEY", 
    project_id="YOUR_PROJECT_ID", 
    auth_headers=[
        {"Authorization": "Bearer YOUR_TOKEN"}
        ]
    )

# Prepare tools
tools = client.get_tools()

completion = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What is the weather like in Paris today?"}],
    # Use tools
    tools=tools
)

response_message = response.choices[0].message
tool_calls = response_message.tool_calls

messages.append(response_message)


for tool_call in tool_calls:
    function_name = tool_call.function.name
 
    function_args = json.loads(tool_call.function.arguments)

    # Call the tool using interlify
    function_response = interlify.call_tool(function_name, function_args)

    messages.append(
        {
            "role": "tool",
            "content": str(function_response),
            "tool_call_id": tool_call.id,
        }
    )

final_response = client.chat.completions.create(
    model=model, messages=messages, tools=tools, tool_choice="auto"
)

print(final_response.choices[0].message.content)

Work with OpenAI Agent SDK

OpenAI just released its Agent SDK. The Agents SDK is designed to be highly flexible, allowing you to model a wide range of LLM workflows including deterministic flows, iterative loops, and more.

Interlify's latest version supports OpenAI agent for tools calling, which makes integrating your APIs or services to the modern agent capability in a flash!

Here is the code:

import asyncio
import os
from dotenv import load_dotenv

from openai import AsyncOpenAI

from agents import (
    Agent,
    OpenAIChatCompletionsModel,
    Runner,
    set_tracing_disabled,
    FunctionTool
)

from interlify import Interlify

load_dotenv()

# Set these variables in your .env file 
BASE_URL = os.getenv("BASE_URL") or ""
API_KEY = os.getenv("API_KEY") or ""
MODEL_NAME = os.getenv("MODEL_NAME") or ""
INTERLIFY_API_KEY = os.getenv("INTERLIFY_API_KEY") or ""
PROJECT_ID = os.getenv("PROJECT_ID") or ""
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN") or ""


if not BASE_URL or not API_KEY or not MODEL_NAME or not INTERLIFY_API_KEY or not PROJECT_ID or not ACCESS_TOKEN:
    raise ValueError(
        "Please set BASE_URL, API_KEY, MODEL_NAME, INTERLIFY_API_KEY, PROJECT_ID, ACCESS_TOKEN via env var or code."
    )

# works for non-openai model as well
client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
# disable tracing if you are using non-openai model
set_tracing_disabled(disabled=True)

# Initialize the client
interlify = Interlify(
    api_key=INTERLIFY_API_KEY,
    project_id=PROJECT_ID,
    auth_headers=[{"Authorization": f"Bearer {ACCESS_TOKEN}"}],
)


# Prepare tools for agent: convert tools to FunctionTool format that agent can use
agent_tools = [
    FunctionTool(
        name=tool["name"],
        description=tool["description"],
        params_json_schema=tool["function"]["parameters"]["properties"],
        on_invoke_tool=tool["tool_function"],
    )
    for tool in interlify.get_tools()
]

async def main():

    agent = Agent(
        name="Assistant",
        instructions="You are a shoe shop assistant.",
        model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client),
        tools=agent_tools,
    )

    result = await Runner.run(agent, "please update the price of the second shoe to 100.")
    print(result.final_output)


if __name__ == "__main__":
    asyncio.run(main())

That's it!

To setup the tools and projects, please visit to interlify website.

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

interlify-1.0.4.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

interlify-1.0.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file interlify-1.0.4.tar.gz.

File metadata

  • Download URL: interlify-1.0.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for interlify-1.0.4.tar.gz
Algorithm Hash digest
SHA256 bf3c20cfb5a120138e378e91864e27175a62b1d5adda943171b595c38000933b
MD5 c40ca8f6c6656265616373a5c43e59e8
BLAKE2b-256 b950bd4ced11e0c536027049767dc9803dc2353787444fc376d637302d3b5a48

See more details on using hashes here.

File details

Details for the file interlify-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: interlify-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for interlify-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ef9fd38022a8fa5e32844c328297e43dbe621506a5c495359ba2a53ff8d324ba
MD5 acc59db90655d78a9304cd3998b25ec2
BLAKE2b-256 1d8dce9d1e8eeefb31e14cfa1bcca0cd9c78f04bd14095608bbbeae4bf537efe

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