Skip to main content

The official Python library for the Integry API

Project description

Integry Python API Library

PyPI version

The Python API library allows access to Integry REST API from Python programs.

Installation

# install from PyPI
pip install integry

Usage with Agent Frameworks

1. LangChain/LangGraph

import os
from integry import Integry
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_openai import ChatOpenAI
from langchain_core.tools import StructuredTool
from langgraph.prebuilt import create_react_agent

user_id = "your user's ID"

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

slack_post_message = await integry.functions.get("slack-post-message", user_id)

llm = ChatOpenAI(
    model="gpt-4o",
    api_key=os.environ.get("OPENAI_API_KEY"),
)

tool = slack_post_message.get_langchain_tool(StructuredTool.from_function, user_id)

agent = create_react_agent(
    tools=[tool],
    model=llm,
)

await agent.ainvoke({
    "messages": [
        SystemMessage(content="You are a helpful assistant"),
        HumanMessage(content="Say hello to my team on slack"),
    ]
})

2. CrewAI

import os
from integry import Integry
from crewai import Agent, Task, Crew, LLM
from crewai.tools.structured_tool import CrewStructuredTool

user_id = "your user's ID"

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

slack_post_message = await integry.functions.get("slack-post-message", user_id)

tools = [
    slack_post_message.get_langchain_tool(CrewStructuredTool.from_function, user_id)
]

llm = LLM(
    model="gpt-4o",
    temperature=0,
    base_url="https://api.openai.com/v1",
    api_key=os.environ.get("OPENAI_API_KEY"),
)

crewai_agent = Agent(
    role="Integration Assistant",
    goal="Help users achieve their goal by performing their required task in various apps",
    backstory="You are a virtual assistant with access to various apps and services. You are known for your ability to connect to any app and perform any task.",
    verbose=True,
    tools=tools,
    llm=llm,
)

task = Task(
    description="Say hello to my team on slack",
    agent=crewai_agent,
    expected_output="Result of the task",
)

crew = Crew(agents=[crewai_agent], tasks=[task])

result = crew.kickoff()

3. AutoGen

import os
from integry import Integry
from autogen import ConversableAgent, register_function

user_id = "your user's ID"

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

function = await integry.functions.get("slack-post-message", user_id)

llm_config = {"config_list": [{"model": "gpt-4o", "api_key": os.environ.get("OPENAI_API_KEY")}]}

assistant = ConversableAgent(
    name="Assistant",
    system_message="You are a helpful integrations assistant. "
    "You can help users perform tasks in various apps. "
    "Return 'TERMINATE' when the task is done.",
    llm_config=llm_config,
)

user_proxy = ConversableAgent(
    name="User",
    llm_config=False,
    is_termination_msg=lambda msg: msg.get("content") is not None
    and "TERMINATE" in msg["content"],
    human_input_mode="NEVER",
)

function.register_with_autogen_agents(
    register_function,
    caller=assistant,
    executor=user_proxy,
    user_id=user_id,
)

chat_result = await user_proxy.a_initiate_chat(
    assistant,
    message="Say hello to my team on slack",
)

4. LlamaIndex

import os
from integry import Integry
from llama_index.core.tools import FunctionTool, ToolMetadata
from llama_index.llms.openai import OpenAI
from llama_index.core.agent import ReActAgent

user_id = "your user's ID"

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

slack_post_message = await integry.functions.get("slack-post-message", user_id)

llm = OpenAI(model="gpt-4o", temperature=0, api_key=os.environ.get("OPENAI_API_KEY"))

tools = [
    slack_post_message.get_llamaindex_tool(FunctionTool.from_defaults, ToolMetadata, user_id)
]

agent = ReActAgent.from_tools(tools=tools, llm=llm, verbose=True)

task = "Say hello to my team on slack."

result = await agent.achat(task)

5. Haystack

import os
from integry import Integry
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.components.tools import ToolInvoker
from haystack.tools import Tool

user_id = "your user's ID"

os.environ.get("OPENAI_API_KEY")

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)


slack_post_message = await integry.functions.get("slack-post-message", user_id)

tool = slack_post_message.get_haystack_tool(Tool, user_id)

chat_generator = OpenAIChatGenerator(model="gpt-4o-mini", tools=[tool])

tool_invoker = ToolInvoker(tools=[tool])

user_message = ChatMessage.from_user("Say hello to my team on slack.")

replies = chat_generator.run(messages=[user_message])["replies"]

if replies[0].tool_calls:
    tool_messages = tool_invoker.run(messages=replies)["tool_messages"]
    print(f"tool messages: {tool_messages}")

6. Smolagents

import os
from smolagents import tool, CodeAgent, HfApiModel
from integry import Integry

user_id = "your user's ID"
hugging_face_token = os.environ.get("HUGGING_FACE_TOKEN")

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

slack_post_message = await integry.functions.get("slack-post-message", user_id)

slack_tool = slack_post_message.get_smolagent_tool(tool, user_id)

agent = CodeAgent(tools=[slack_tool], model=HfApiModel(token=hugging_face_token))

agent.run("Say hello to my team on slack.")

7. Litellm

import litellm
from integry import Integry
from integry import handle_litellm_tool_calls

user_id = "your user's ID"

os.environ.get("OPENAI_API_KEY")

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

slack_post_message = await integry.functions.get("slack-post-message", user_id)

messages = [{"role": "user", "content": "Say hello to my team on slack."}]

response = await litellm.acompletion(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    tools=[slack_post_message.get_litellm_tool()],
    tool_choice="auto",
)

await handle_litellm_tool_calls(response, user_id, [slack_post_message])

8. Mistral AI

from mistralai import Mistral
from integry import Integry
from integry import handle_mistralai_tool_calls

user_id = "your user's ID"

api_key = os.environ.get("MISTRAL_API_KEY")
model = "mistral-large-latest"
client = Mistral(api_key=api_key)


# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

slack_post_message = await integry.functions.get("slack-post-message", user_id)

messages = [{"role": "user", "content": "Say hello to my team on slack."}]

response = await client.chat.complete_async(
    model=model,
    messages=messages,
    tools=[slack_post_message.get_mistralai_tool()],
    tool_choice="auto"
)

await handle_mistralai_tool_calls(response, user_id, [slack_post_message])

Prediction

import os
from integry import Integry

user_id = "your user's ID"

# Initialize the client
integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

# Get the most relevant function
predictions = await integry.functions.predict(
    prompt="say hello to my team on Slack", user_id=user_id, predict_arguments=True
)

if predictions:
    function = predictions[0]
    # Call the function
    await function(user_id, function.arguments)

Calling a function with a specific connected account

If a user has multiple connected accounts for the same app, pass connected_account_id to target a specific account when calling a function:

await integry.functions.call(
    "slack-post-message",
    arguments={"channel": "#general", "text": "Hello"},
    user_id=user_id,
    connected_account_id=12345,
)

# Or via the Function instance:
await function(
    user_id,
    arguments={"channel": "#general", "text": "Hello"},
    connected_account_id=12345,
)

Pagination

List methods are paginated and allow you to iterate over data without handling pagination manually.

from integry import Integry

user_id = "your user's ID"

integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

async for function in integry.functions.list(user_id):
    # do something with function
    print(function.name)

If you want to control pagination, you can fetch individual pages by using the cursor returned by the previous page.

from integry import Integry

user_id = "your user's ID"

integry = Integry(
    app_key=os.environ.get("INTEGRY_APP_KEY"),
    app_secret=os.environ.get("INTEGRY_APP_SECRET"),
)

first_page = await integry.apps.list(user_id)

second_page = await integry.apps.list(user_id, cursor=first_page.cursor)

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

integry-0.0.15.tar.gz (70.3 kB view details)

Uploaded Source

Built Distribution

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

integry-0.0.15-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for integry-0.0.15.tar.gz
Algorithm Hash digest
SHA256 1e9c5d501be73a88a5796118cc1a15b696f0ea09222f5042f4dde03463db7080
MD5 9d45b0e7ecb46b7c1d725381829daa89
BLAKE2b-256 a4a775e78de141206a00f65fc0c9a8fcd8b40773c99a986ec94211348129ff94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: integry-0.0.15-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.12

File hashes

Hashes for integry-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 09e085c2d9b21791794cf3d411d3f9ea0aae091dda785d2a6f2c6dee4f6de25b
MD5 fdc3ed9f73a89bebe86fd8aad1e46b92
BLAKE2b-256 d5c79157ef5870d0fd0b09cc885145aae0e7b87496760263bf5e92f440a35c93

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