I am using langchain to chat with my database I want json format as output which includes fieldname as key

huangapple go评论73阅读模式
英文:

I am using langchain to chat with my database I want json format as output which includes fieldname as key

问题

以下是翻译好的部分:

"I am working on Natural language to query your SQL Database using LangChain powered by ChatGPT." -> "我正在使用由ChatGPT提供支持的LangChain来进行自然语言查询您的SQL数据库。"

"I am using Langchain's SQL database to chat with my database, it returns answers in the sentence I want the answer in JSON format so I have designed a prompt but sometimes it is not giving the proper format." -> "我正在使用Langchain的SQL数据库与我的数据库进行交互,它返回的答案是我想要的句子,并以JSON格式返回,因此我设计了一个提示,但有时它未提供正确的格式。"

"I have a projects table in my DB that has id, project_name, area, price, bhk, and amenities fields." -> "我的数据库中有一个名为projects的表,其中包含id、'project_name'、area、price、'bhk'和amenities字段。"

"Q="project in Bandra area"" -> "Q="在班德拉地区的项目""

"Ans="{'id': 5, 'project_name': 'Anantara Abode', 'area': 'Bandra', 'price': 4500000, 'bhk': 3, 'amenities': 'None'}"" -> "Ans="{'id': 5, 'project_name': 'Anantara Abode', 'area': 'Bandra', 'price': 4500000, 'bhk': 3, 'amenities': 'None'}""

"Q="Flats under 65 lakhs"" -> "Q="65万卢比以下的公寓""

"A={1: {'project_name': 'Anantara', 'area': 'Shahibaug', 'price': 5000000, 'bhk': 3, 'amenities': 'Club house,Swimming Pool'},..." -> "A={1: {'project_name': 'Anantara', 'area': 'Shahibaug', 'price': 5000000, 'bhk': 3, 'amenities': 'Club house,Swimming Pool'},..."

"In the second answer "id" field name is not included as a key." -> "在第二个答案中,“id”字段名称没有包含为键。"

"%%capture" 不需要翻译。

"!pip install -q langchain" 不需要翻译。

"!pip install -q openai" 不需要翻译。

"from langchain import OpenAI , SQLDatabase , SQLDatabaseChain" 不需要翻译。

"from sqlalchemy import create_engine" 不需要翻译。

"input_db= SQLDatabase.from_uri('sqlite:///main.db')" -> "input_db= SQLDatabase.from_uri('sqlite:///main.db')"

"llm_1=OpenAI(openai_api_key=openai_api_key,temperature=0)" -> "llm_1=OpenAI(openai_api_key=openai_api_key,temperature=0)"

"db_agent=SQLDatabaseChain(llm = llm_1,database = input_db,verbose=True)" -> "db_agent=SQLDatabaseChain(llm = llm_1,database = input_db,verbose=True)"

"QUERY ="""..." 不需要翻译。

"question = QUERY.format(question="project in Bandra area")" -> "question = QUERY.format(question="在班德拉地区的项目")"

英文:

I am working on Natural language to query your SQL Database using LangChain powered by ChatGPT.I am using Langchain's SQL database to chat with my database, it returns answers in the sentence I want the answer in JSON format so I have designed a prompt but sometimes it is not giving the proper format. I have a projects table in my DB that has id, project_name, area, price, bhk, and amenities fields.

Q="project in Bandra area"  
Ans="{'id': 5, 'project_name': 'Anantara Abode', 'area': 'Bandra', 'price': 4500000, 'bhk': 3, 'amenities': 'None'}"  
Q="Flats under 65 lakhs"  
A={1: {'project_name': 'Anantara', 'area': 'Shahibaug', 'price': 5000000, 'bhk': 3, 'amenities': 'Club house,Swimming Pool'},  
2: {'project_name': 'Vivanta', 'area': 'Shilaj', 'price': 7000000, 'bhk': 2, 'amenities': 'Club house, Swimming Pool'},  
3: {'project_name': 'Athence', 'area': 'SG highway', 'price': 5500000, 'bhk': 2, 'amenities': 'Garden, Temple'},  
4: {'project_name': 'Anantara Alpines', 'area': 'Hebatpur', 'price': 6000000, 'bhk': 3, 'amenities': 'None'},  
5: {'project_name': 'Anantara Abode', 'area': 'Bopal', 'price': 4500000, 'bhk': 3, 'amenities': 'None'}}

In the second answer "id" field name is not included as a key.

%%capture  
!pip install -q langchain  
!pip install -q openai 
from langchain import OpenAI , SQLDatabase , SQLDatabaseChain  
from langchain.chat_models import ChatOpenAI

from sqlalchemy import create_engine  
input_db= SQLDatabase.from_uri('sqlite:///main.db')  
llm_1=OpenAI(openai_api_key=openai_api_key,temperature=0)

db_agent=SQLDatabaseChain(llm = llm_1,database = input_db,verbose=True)

QUERY ="""
Given an input question, first create a syntactically correct sql query to run, then look at the results      of the query and return the answer in dictonary format and include id fieldname in all answers.
Use the following format.  
Question: "Question here"   
SQLQuery: "SQL Query to run include id in all queries"  
SQLResult: "Result of the SQLQuery include id in all result"  
Answer: "Final answer should be in dictionary format include id fieldname in all answers"  
{question}"""

question = QUERY.format(question="project in Bandra area")   
db_agent.run(question)

答案1

得分: 1

尝试使用示例设置您的提示,将其添加到您的提示中:

"最终答案应采用以下格式:
{“data”:[{“column1”:“value1”,“column2”:“value2”,“column3”:“value3”,...},“2”:{“column1”:“value1”,“column2”:“value2”,“column3”:“value3”,...},...]}
确保所有字符串都用双引号括起来。
例如:
{“data”:[{“id”:“1”,“project_name”:“Vivanta”,“area”:“Shilaj”,...},“3”:{“id”:“3”,“project_name”:“Athence”,“area”:“SG Highway”,...},...}]}"

请注意,我已经将示例代码中的引号从HTML实体引号(")更改为标准的双引号。

英文:

Try to setup your prompt with an example, add this in your prompt

"Final answer should be in the following format:

{"data": [{"column1": "value1", "column2": "value2", "column3": "value3",...},"2": {"column1": "value1", "column2": "value2", "column3": "value3",...}, ...}]}

Ensure that all strings are enclosed in double quotes.

For example:
{"data":[{"id":"1","project_name":"Vivanta","area":"Shilaj",...},{"id":"3","project_name":"Athence","area":"SG Highway",...}, ...}]}"

huangapple
  • 本文由 发表于 2023年7月3日 14:36:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定