Skip to content

Commit

Permalink
🤖 openai + textinput blocks / logic added (#1096)
Browse files Browse the repository at this point in the history
* 🤖 openai + textinput blocks / logic added

* lint fixes

* made langchain call async

* renamed method

* summary agent refactor

* black reformat
  • Loading branch information
shahrishabh7 authored Jul 19, 2023
1 parent 8e721ad commit 5fec6d7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
46 changes: 45 additions & 1 deletion next/src/services/workflow/node-block-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ const SlackWebhookBlockDefinition: NodeBlockDefinition = {
],
};

const SummaryWebhookBlockDefinition: NodeBlockDefinition = {
name: "Summary Agent",
type: "SummaryWebhook",
description: "Summarize or extract key details from text using OpenAI",
image_url: "/tools/web.png",
input_fields: [
{
name: "prompt",
description: "What do you want to do with the text?",
type: "string"
}
],
output_fields: [
{
name: "result",
description: "The result was built.",
type: "string",
},
],
};

const TextInputWebhookBlockDefinition: NodeBlockDefinition = {
name: "Text Input",
type: "TextInputWebhook",
description: "",
image_url: "/tools/web.png",
input_fields: [
{
name: "text",
description: "What text would you like to extract information from?",
type: "string"
}
],
output_fields: [
{
name: "result",
description: "The result was built.",
type: "string",
},
],
};

const IfBlockDefinition: NodeBlockDefinition = {
name: "If Block",
type: "IfBlock",
Expand All @@ -92,9 +134,11 @@ export const getNodeBlockDefinitions = () => {
SlackWebhookBlockDefinition,
IfBlockDefinition,
TriggerBlockDefinition,
SummaryWebhookBlockDefinition,
TextInputWebhookBlockDefinition
];
};

export const getNodeBlockDefinitionFromNode = (node: Node<WorkflowNode>) => {
return getNodeBlockDefinitions().find((d) => d.type === node.data.block.type);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SlackWebhook(Block):
input: SlackWebhookInput

async def run(self) -> BlockIOBase:
logger.info(f"Starting {self.type}")
logger.info(f"Starting {self.type} with {self.input.message}")

try:
response = requests.post(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
from loguru import logger
from reworkd_platform.web.api.agent.model_settings import create_model
from reworkd_platform.schemas.user import UserBase
from reworkd_platform.schemas.agent import ModelSettings
from langchain import LLMChain, PromptTemplate
from lanarky.responses import StreamingResponse

from reworkd_platform.schemas.workflow.base import Block, BlockIOBase


class SummaryWebhookInput(BlockIOBase):
prompt: str


class SummaryWebhookOutput(SummaryWebhookInput):
result: str


class SummaryWebhook(Block):
type = "SummaryWebhook"
description = "Extract key details from text using OpenAI"
input: SummaryWebhookInput

async def run(self) -> BlockIOBase:
logger.info(f"Starting {self.type}")

try:
response = await summarize_and_extract(self.input.prompt)
logger.info(f"RESPONSE {response}")
except Exception as err:
logger.error(f"Failed to extract text with OpenAI: {err}")
raise

return SummaryWebhookOutput(**self.input.dict(), result=response)


async def summarize_and_extract(prompt: str) -> str:
llm = create_model(
ModelSettings(),
UserBase(id="", name=None, email="[email protected]"),
streaming=False,
)
template = """
You are a chatbot assistant that assists users in summarizing and extracting information from given text.
Question: {prompt}
Answer:"""

chain = LLMChain(
llm=llm, prompt=PromptTemplate(template=template, input_variables=["prompt"])
)

result = await chain.arun(prompt)
return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
from loguru import logger

from reworkd_platform.schemas.workflow.base import Block, BlockIOBase


class TextInputWebhookInput(BlockIOBase):
text: str


class TextInputWebhookOutput(BlockIOBase):
result: str


class TextInputWebhook(Block):
type = "TextInputWebhook"
description = "Enter Text to extract key details from"
input: TextInputWebhookInput

async def run(self) -> BlockIOBase:
logger.info(f"Starting {self.type}")

return TextInputWebhookOutput(result=self.input.text)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from reworkd_platform.schemas.workflow.blocks.manual_trigger import ManualTriggerBlock
from reworkd_platform.schemas.workflow.blocks.slack_webhook import SlackWebhook
from reworkd_platform.schemas.workflow.blocks.status_check import UrlStatusCheckBlock
from reworkd_platform.schemas.workflow.blocks.summarization_webhook import (
SummaryWebhook,
)
from reworkd_platform.schemas.workflow.blocks.text_input_webhook import TextInputWebhook


def get_block_runner(block: Block) -> Block:
Expand All @@ -11,5 +15,9 @@ def get_block_runner(block: Block) -> Block:
return UrlStatusCheckBlock(**block.dict())
if block.type == "SlackWebhook":
return SlackWebhook(**block.dict())
if block.type == "TextInputWebhook":
return TextInputWebhook(**block.dict())
if block.type == "SummaryWebhook":
return SummaryWebhook(**block.dict())
else:
raise ValueError(f"Unknown block type: {block.type}")

0 comments on commit 5fec6d7

Please sign in to comment.