Sema4AI / actions

AI Actions connect Agents to your existing enterprise systems and applications - all in 🐍 Python.
https://sema4.ai/
Apache License 2.0
45 stars 5 forks source link
agents ai ai-actions python

Sema4ai

[Docs](https://robocorp.com/docs) | [Blog](https://medium.com/sema4-ai) | [Examples](https://github.com/sema4ai/cookbook) | [CodeGen](https://chat.robocorp.com) | [Slack](https://sema4ai-users.slack.com/) | [Youtube](https://www.youtube.com/@Robocorp) | [𝕏](https://twitter.com/sema4ai)

PyPI - Version PyPI - Version GitHub issues License

NOTE:
This project started as Robocorp Action Server, and is currently being migrated under Sema4.ai organization. You will still likely find links to Robocorp resources. It's all the same company!

Build Semantic Actions that connect AI Agents with the real-world - all in 🐍 Python.

Sema4.ai is the easiest way to extend the capabilities of AI agents, assistants and copilots with custom actions, written in Python. Create and deploy tools, skills, loaders and plugins that securely connect any AI Assistant platform to your data and applications.

Sema4.ai Action Server makes your Python scripts compatible with e.g. OpenAI's custom GPTs, LangChain and OpenGPTs by automatically creating and exposing an API based on function declaration, type hints and docstrings. Just add @action and start!

Text changing depending on mode. Light: 'So light!' Dark: 'So dark!'

🏃‍♂️ Quickstart

There are two main ways using the Action Server: use the command line, or with our VS Code extension. This section gets you going!

CLI For macOS ```sh brew update brew install sema4ai/tools/action-server ```
CLI For Windows ```sh # Download Sema4.ai Action Server curl -o action-server.exe https://cdn.sema4.ai/action-server/releases/latest/windows64/action-server.exe ``` You can download/move the executable into a folder that is in your `PATH`, or you can [add the folder into PATH](https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/) so that you can call `action-server` wherever you are.
CLI For Linux ```sh # Download Sema4.ai Action Server curl -o action-server https://cdn.sema4.ai/action-server/releases/latest/linux64/action-server chmod a+x action-server # Add to PATH or move to a folder that is in PATH sudo mv action-server /usr/local/bin/ ```
Robocorp Code extension for VS Code > **NOTE:** > Robocorp VS Code extension is in the process to be renamed to Sema4.ai extension. After installing [Robocorp Code extension from the VS Code Markeplace](https://marketplace.visualstudio.com/items?itemName=robocorp.robocorp-code), open the Command Palette (`Command-Shift-P` or `Ctrl-Shift-P`) and select `Robocorp: Create Action Package`. This will bootstrap a new project. You can then run/debug indvidual Actions from the Extension's sidebar, or start the Action Server. ![github-extension](https://github.com/robocorp/robocorp/assets/40179958/d53000bf-558e-48a7-bb30-4610b9bf24c5)


Bootstrap a new project from a template. You’ll be prompted for the name of the project:

action-server new

Navigate to the freshly created project folder and start the server:

cd my-project
action-server start --expose

👉 You should now have an Action Server running locally at: http://localhost:8080, to open the web UI.

👉 Using the --expose -flag, you also get a public internet-facing URL (something like twently-cuddly-dinosaurs.sema4ai.link) and an API key. These are the details that you need to configure your AI Agent.

Head over to Action Server docs for more.


What makes a Python function an⚡️Action?

1️⃣ package.yaml file that describes the set of Actions your are working on, and defines up your Python environment and dependencies:

name: Package name
description: Action package description
documentation: https://github.com/...

dependencies:
  conda-forge:
    - python=3.10.12
    - uv=0.1.37
  pypi:
    - sema4ai-actions=0.3.1
    - pytz=2024.1
🙋‍♂️ "Why not just pip install...?" Think of this as an equivalent of the requirements.txt, but much better. 👩‍💻 With `package.yaml` you are not just controlling your PyPI dependencies, you control the complete Python environment, which makes things repeatable and easy. 👉 You will probably not want run the Actions just on your machine, so by using `package.yaml`: - You can avoid `Works on my machine` -cases - You do not need to manage Python installations on all the machines - You can control exactly which version of Python your automation will run on - ..as well as the pip version to avoid dependency resolution changes - No need for venv, pyenv, ... tooling and knowledge sharing inside your team. - Define dependencies in package.yaml let our tooling do the heavy lifting. - You get all the content of [conda-forge](https://prefix.dev/channels/conda-forge) without any extra tooling > The environment management is provided by another open-source project of ours, [RCC](https://github.com/robocorp/rcc).


2️⃣ @action decorator that determines the action entry point and Type hints and docstring to let AI agents know what the Action does in natural language.

@action
def greeting(name: str) -> str:
    """
    Greets the user

    Args:
        name (str): The user name

    Returns:
        str: Final user greeting
    """

Connect with OpenAI GPTs Actions

Once you have started the Action Server with --expose flag, you’ll get a URL available to the public, along with the authentication token. The relevant part of the output from the terminal looks like this, of course with your own details:

...
Uvicorn running on http://localhost:8080 (Press CTRL+C to quit)
🌍 URL: https://seventy-six-helpless-dragonflies.sema4ai.link
🔑 Add following header api authorization header to run actions: { "Authorization": "Bearer xxx_xxx" }

👉 Example video in Youtube 👈

Adding the Action Server-hosted AI Action to your custom GPT is super simple: basically just navigate to “Actions” section of the GPT configuration, add the link to import the actions, and Add Authentication with Authentication method set to “API key” and Auth Type to “Bearer”.

TIP:
Use the @action(is_consequential=False) flag to avoid the user needing to accept the action execution separately each time on your GPT.

Add Action Server as a Toolkit to 🦜️🔗 LangChain

NOTE:
This section is still under Robocorp, but pending rename to Sema4.ai soon.

Sema4.ai Action Server has everything needed to connect it to your Langchain AI app project. The easiest way is to start with the template provided in the Langchain project. Here’s how to do it:

# Install LangChain cli tool if not already there
pip install langchain-cli

# Create a new LangChain app using Action Server template
langchain app new my-awesome-app --package robocorp-action-server

Then define the route inside the created ./my-awesome-app/app/server.py file:

from langserve import add_routes
+ from robocorp_action_server import agent_executor as action_server_chain

# Edit this to add the chain you want to add
- add_routes(app, NotImplemented)
+ add_routes(app, action_server_chain, path="/robocorp-action-server")

After the setup make sure you have:

Finally, inside the project directory ./my-awesome-app spin up a LangServe instance directly by:

langchain serve

After running the steps above, you’ll have a Playground available at http://127.0.0.1:8000/robocorp-action-server/playground/ where you can test your Actions with an AI agent.

Want to build your own thing? Adding your Robocorp AI Actions to a Langchain project is as easy as the code below. Just remember to change the URL of the Action Server if you are not running both the Action Server and Langchain app on the same machine.

from langchain_robocorp import ActionServerToolkit

# Initialize Action Server Toolkit
toolkit = ActionServerToolkit(url="http://localhost:8080")
tools = toolkit.get_tools()

Why use Sema4.ai AI Actions

Sema4.ai stack is hands down the easiest way to give AI agents more capabilities. It’s an end-to-end stack supporting every type of connection between AI and your apps and data. You are in control where to run the code and everything is built for easiness, security, and scalability.

Inspiration

Check out these example projects for inspiration.

Build more @actions and be awesome! We'd love to hear and see what have you built. Join our Slack community to share your work.

Contributing and issues

⭐️ First, please star the repo - your support is highly appreciated!

Contributors

Contributors