import json from langchain_core.prompts import MessagesPlaceholder from langchain_core.prompts import ChatPromptTemplate from langchain_core.messages import AIMessage, HumanMessage from langchain.agents.format_scratchpad.openai_tools import ( format_to_openai_tool_messages, ) from langchain import PromptTemplate, LLMChain import os from dotenv import load_dotenv from langchain.memory import ConversationBufferWindowMemory from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate from langchain.chains import ConversationChain from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory from pymongo import MongoClient from langchain_openai import ChatOpenAI load_dotenv() default_json = {} class FormFillingBot: def __init__(self): self.llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=os.getenv("OPENAI_API_KEY"), temperature=0) def get_current_form_json(self,uuid): CONNECTION_STRING = "mongodb+srv://dharasolanki:8apF57ZCeX16fKny@cluster0.egh8f3k.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" # Connect to the MongoDB client client = MongoClient(CONNECTION_STRING) # Select the database and collection db = client['benefitboost'] collection = db['currentFormJson'] # The UUID to search for uuid_to_find = uuid # Find the document with the specified UUID document =collection.find_one({"uuid": uuid_to_find}) if document: currentformjson = document.get('currentformjson', None) if currentformjson: print("Current Form JSON:", currentformjson) else: print("The 'currentformjson' field is not found in the document.") else: print(f"No document found with UUID: {uuid_to_find}") return currentformjson def form_filling(self, form_data,default_json): "Use this tool for form filling task" friendly_prompt = PromptTemplate( input_variables=["user_input", "default_json"], template="""You are a form-filling expert that takes the data from user input and finds the related field in {default_json} and returns the json provided with the details of the user. If you do not encounter fields of form then give response as you are a question-answering bot. If you do not find the details put it blank, do not try to fill every detail if it is not present in user_input. Here are the details entered by the user: {user_input}""" ) friendly_chain = LLMChain(llm=self.llm, prompt=friendly_prompt) response = friendly_chain.invoke({"user_input": form_data, "default_json": default_json}) return response["text"]