Skip to content

Commit

Permalink
Fix/docs-processor (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
sohamganatra authored Aug 16, 2024
1 parent 21484af commit b932d99
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
71 changes: 71 additions & 0 deletions docs/introduction/foundations/components/actions/processing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "🛠️ Enhancing Action Inputs & Outputs"
sidebarTitle: "Processing Actions"
icon: "wand-magic-sparkles"
description: "Master the art of preprocessing and postprocessing actions for optimal results."
---

## Refining Action Inputs & Outputs

In many scenarios, the raw inputs or outputs of actions may benefit from additional processing. This refinement step can significantly improve the quality and usability of your data. Here are two key use cases:

- **Postprocessing**: Streamline large action responses by filtering or formatting the data before it reaches the Language Model (LLM).
- **Preprocessing**: Generate or modify inputs dynamically at runtime, handling scenarios that may be challenging for the LLM to produce directly.

Composio empowers you with the ability to define **custom Python functions** as preprocessors or postprocessors.

These can be applied at two levels:

1. **Tool-level**: Affects all actions within a specific tool.
2. **Action-level**: Tailored processing for individual actions.

Here's how you can implement these processors:
<Steps>
<Step title="Define the Preprocessor or Postprocessor Functions">
<CodeGroup>
```python Define the Preprocessor or Postprocessor Functions
def tool_preprocessor(input_data):
# Modify input_data as needed
return modified_input_data

def tool_postprocessor(output_data):
# Process output_data as needed
return processed_output_data

def action_preprocessor(input_data):
# Modify input_data as needed
return modified_input_data

def action_postprocessor(output_data):
# Process output_data as needed
return processed_output_data
```
</CodeGroup>
</Step>
<Step title="Use them while creating the toolset">
<CodeGroup>
```python Set the pre and post processors while creating the toolset
# while defining the toolset, you can define the pre and post processors
composio_toolset = ComposioToolSet(
processors={
"pre": {
App.GITHUB: tool_preprocessor,
Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER: action_preprocessor,
},
"post": {
App.GITHUB: tool_postprocessor,
Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER: action_postprocessor,
},
},
)

tools = composio_toolset.get_tools(apps=[App.GITHUB])
```
</CodeGroup>
</Step>
</Steps>


<Warning>
Ensure that your preprocessing and postprocessing functions are efficient and don't introduce significant latency.
</Warning>
34 changes: 11 additions & 23 deletions docs/introduction/intro/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const composio_toolset = new OpenAIToolSet({
<Tab title="Python">
<CodeGroup>
```python Get GitHub action for starring a repo
actions = composio_toolset.get_actions(actions=[Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER])
tools = composio_toolset.get_actions(actions=[Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER])
```
</CodeGroup>
</Tab>
Expand All @@ -115,7 +115,8 @@ actions: ["github_start_repo"]
<Tab title="Python">
<CodeGroup>
```python Star a repository using LLM
my_task = "Star a repo composiohq/composio on GitHub"
task = "Star a repo composiohq/composio on GitHub"

response = openai_client.chat.completions.create(
model="gpt-4-turbo-preview",
tools=tools,
Expand All @@ -124,8 +125,6 @@ response = openai_client.chat.completions.create(
{"role": "user", "content": task},
],
)
print(response)

```
</CodeGroup>
</Tab>
Expand Down Expand Up @@ -185,6 +184,7 @@ Replace `OPENAIKEY`.
<Tab title="Python">
<CodeGroup>
```python Complete Code

## Step 1
#!pip install composio_core
#!pip install composio_openai
Expand All @@ -194,34 +194,22 @@ Replace `OPENAIKEY`.

## Step 3
from openai import OpenAI
from composio_openai import ComposioToolSet, App,Action
from composio_openai import ComposioToolSet, App, Action

openai_client = OpenAI(api_key="******OPENAIKEY******")
openai_client = OpenAI()

# Initialise the Composio Tool Set
composio_toolset = ComposioToolSet(api_key="**\*\***COMPOSIO_API_KEY**\*\***")
composio_toolset = ComposioToolSet()

## Step 4
# Get GitHub tools that are pre-configured
actions = composio_toolset.get_actions(actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER])

tools = composio_toolset.get_actions(
actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER]
)

## Step 5
my_task = "Star a repo composiohq/composio on GitHub"
openai_client = OpenAI()
composio_toolset = ComposioToolSet()

# Define task.
task = "Star a repo composiohq/composio on GitHub"

# Get GitHub tools that are pre-configured
tools = composio_toolset.get_tools(apps=[App.GITHUB])

# Extension of system prompt(Not using at this moment)
_ = composio_toolset.get_agent_instructions(
apps=[App.GITHUB],
)

# Get response from the LLM
response = openai_client.chat.completions.create(
model="gpt-4-turbo-preview",
Expand All @@ -231,10 +219,10 @@ response = openai_client.chat.completions.create(
{"role": "user", "content": task},
],
)
print(response)

# Execute the function calls.
result = composio_toolset.handle_tool_calls(response)

print(result)
```
</CodeGroup>
Expand Down
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
{
"group": "Advanced",
"pages": [
"introduction/foundations/components/actions/processing",
"introduction/foundations/components/integrations/integration-guide",
"introduction/foundations/components/workspace",
"introduction/foundations/components/integrations/custom-integration"
Expand Down

0 comments on commit b932d99

Please sign in to comment.