Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAG and Tool usage , at the same time #1955

Open
aditya11ad opened this issue May 23, 2024 · 2 comments
Open

RAG and Tool usage , at the same time #1955

aditya11ad opened this issue May 23, 2024 · 2 comments
Labels
question Further information is requested

Comments

@aditya11ad
Copy link

i have a telecom use case:

  1. RAG to general broadband related queries.
    eg.:
    user >> "What is the maximum speed your broadband offers?"
    bot << "We offer 1gbps"

  2. tool usage (function calling) for :
    eg.:
    user >> "show me my mobile data balance?"
    <bot calls a function get_mobile_data_balance(mobile_number)>
    bot << "plz enter ur moile number"
    user >> 9876678978
    <call send_msg(9876678978)>
    bot<< "plz enter OTP sent to u"
    user >> 8822
    <call otp_validation_and_return_data(8822)>
    bot<< "ur data balance is 23GB"


so basically it is a mixture of both , and the agent decides what to invoke :
my way of doing it:
tools = [retrive_tool, get_mobile_data_balance]

Q1. How can i achieve this in langflow?

Q2. After invoking get_mobile_data_balance, how can i maintain the control flow with proper conversation as shown in eg.

Copy link

dosubot bot commented May 23, 2024

To achieve a mixture of Retrieval-Augmented Generation (RAG) and tool usage in Langflow, and maintain the control flow with proper conversation, follow these steps:

1. Setting Up the RAG Model and Tools

First, set up your RAG model and define the tools for specific function calls.

Define the Retriever Tool

Create a retriever tool using the RetrieverToolComponent:

from langchain.tools.retriever import create_retriever_tool
from langflow import CustomComponent
from langflow.field_typing import BaseRetriever, Tool

class RetrieverToolComponent(CustomComponent):
    display_name = "RetrieverTool"
    description = "Tool for interacting with retriever"

    def build_config(self):
        return {
            "retriever": {
                "display_name": "Retriever",
                "info": "Retriever to interact with",
                "type": BaseRetriever,
            },
            "name": {"display_name": "Name", "info": "Name of the tool"},
            "description": {"display_name": "Description", "info": "Description of the tool"},
        }

    def build(
        self,
        retriever: BaseRetriever,
        name: str,
        description: str,
    ) -> Tool:
        return create_retriever_tool(
            retriever=retriever,
            name=name,
            description=description,
        )

Define the Function for Mobile Data Balance

Implement the function get_mobile_data_balance and other related functions:

def get_mobile_data_balance(mobile_number):
    # Logic to get mobile data balance
    balance = "23GB"  # Example balance
    return balance

def send_msg(mobile_number):
    # Logic to send OTP
    otp = "8822"  # Example OTP
    return otp

def otp_validation_and_return_data(otp):
    # Logic to validate OTP and return data balance
    if otp == "8822":
        return "Your data balance is 23GB"
    else:
        return "Invalid OTP"

2. Maintaining Control Flow

To maintain the control flow and handle the conversation properly, use a state management system like GraphStateManager.

Example Conversation Flow

Here is an example of how you can handle the conversation flow:

from langflow.graph.state_manager import GraphStateManager

state_manager = GraphStateManager()

def conversation_flow(user_input, user_id):
    state = state_manager.get_state(user_id)
    
    if state == "awaiting_mobile_number":
        mobile_number = user_input
        send_msg(mobile_number)
        state_manager.update_state(user_id, "awaiting_otp")
        return "Please enter the OTP sent to you"
    
    elif state == "awaiting_otp":
        otp = user_input
        response = otp_validation_and_return_data(otp)
        state_manager.update_state(user_id, "default")
        return response
    
    elif "data balance" in user_input:
        state_manager.update_state(user_id, "awaiting_mobile_number")
        return "Please enter your mobile number"
    
    else:
        # Handle general broadband-related queries using RAG model
        response = retriever_tool.retrieve(user_input)
        return response

# Example integration
retriever_tool = RetrieverToolComponent().build(
    retriever=my_retriever_instance,
    name="MyRetrieverTool",
    description="A tool to retrieve information"
)

# Simulate a user input
user_id = "12345"
user_input = "Can you tell me my data balance?"
response = conversation_flow(user_input, user_id)
print(response)  # Output: Please enter your mobile number

Summary

  1. Define the Retriever Tool: Use the RetrieverToolComponent to create a retriever tool for general queries.
  2. Implement Functions: Define the functions get_mobile_data_balance, send_msg, and otp_validation_and_return_data.
  3. Manage State: Use GraphStateManager to manage the conversation state and maintain control flow.

By following these steps, you can achieve a mixture of RAG and tool usage in Langflow, ensuring that the agent can handle both general queries and specific function calls while maintaining a coherent conversation flow.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@dosubot dosubot bot added the question Further information is requested label May 23, 2024
@YamonBot
Copy link
Contributor

you can run an agent in the Agents section, but you will need to define the two tools you mentioned using custom components.

Contrary to DosuBot's response, if you declare the output type of the custom component as AgentExecutor, you can declare and control the agent directly as you wish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants