main.py
llm_openai = ChatOpenAI(openai_api_key=OPEN_AI_KEY, model='gpt-3.5-turbo-0125')
table_schema = ConfigData.TABLE_SCHEMA
schema_description = ConfigData.SCHEMA_DESCRIPTION
json_ex_1 = ConfigData.FEW_SHOT_EXAMPLE_1
json_ex_string = json.dumps(json_ex_1)
prompt_template_for_creating_query = """
You are an expert in crafting NoSQL queries for MongoDB with 10 years of experience, particularly in MongoDB.
I will provide you with the table_schema and schema_description in a specified format.
Your task is to read the user_question, which will adhere to certain guidelines or formats, and create a NOSQL MongoDb pipeline accordingly.
Table schema:""" + table_schema + """
Schema Description: """ + schema_description + """
Here are some example:
Input: Find movies by Quentin Tarantino and include a count of how many actors are in each movie
Output: {json_ex_string_1}
Note: You have to just return the query nothing else. Don't return any additional text with the query.
Input: {user_question}
"""
query_creation_prompt = PromptTemplate(
template=prompt_template_for_creating_query,
input_variables=["user_question", "json_ex_string_1"],
)
llmchain = LLMChain(llm=llm_openai, prompt=query_creation_prompt, verbose=True)
def get_query(user_question):
response = llmchain.invoke({
"user_question": user_question,
"json_ex_string_1": json_ex_string
})
response_text = response['text'].replace("Output: ", "")
pattern = r'db.collectionName.aggregate(s*['
output_string = re.sub(pattern, '', response_text)
return json.loads(output_string)
client = pymongo.MongoClient(ConfigData.MONGO_DB_URI)
db = client[ConfigData.DB_NAME]
collection_name = db[ConfigData.COLLECTION_NAME]
query_1 = get_query(user_question="when InnovateTech Solutions founded?")
getting error :
ValueError: Missing some input keys: {'n "_id"'}
am trying to generate MongoDB aggregation queries using LangChain with OpenAI’s GPT-3.5. However, I am encountering a ValueError
stating that some input keys are missing.
Here is my setup:
-
I have a
PromptTemplate
that should generate MongoDB queries based on a given schema and user question. -
I invoke the chain using the
invoke
method.