DEV Community

pro01
pro01

Posted on

needed assistance in creating users sessions

I have created a Whatsapp chatbot with help of Openai backend and langchain rag application and I used Twilio as a Whatsapp API provider. For example when two users initiated the conversation. The two users' conversation gets into the same chat interface and this make it the same chat for both of them. I need help creating a separate session for each user and any tips on how I can improve my code for production-ready deployments

from function import text_complition, ref_prompt
from twilio_api import send_message
from flask import Flask, request, session

app = Flask(__name__)
app.secret_key = 'mall_data'

@app.route('/')
def home():
    return 'All is well...'

@app.route('/twilio/receiveMessage', methods=['POST'])
def receiveMessage():
    try:
        # Extract incoming parameters from Twilio
        message = request.form['Body']
        sender_id = request.form['From']

        # Get response from OpenAI
        #refind = ref_prompt(message)
        result = text_complition(message)

        # print(result['response'])
        if result['status'] == 1:
            send_message(sender_id, result['response'])
    except:
        pass

    return 'OK', 200

if __name__ == '__main__':
    app.run()

Enter fullscreen mode Exit fullscreen mode

The above code is the Flask application for receiving the twilio inputs and the below code is the function generating response

def text_complition(query: str) -> dict:
  tools = [tool]

  try:
    agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
    agent_executor = AgentExecutor(agent=agent,
                                   tools=tools,
                                   memory=memory,
                                   verbose=False)
    response = agent_executor.run(query)
    return {"status": 1, "response": response}
  except:
    return {"status": 0, "response": ""}

Enter fullscreen mode Exit fullscreen mode

Thank you in Advance!!

Top comments (0)