基于Python、LangChain、OpenAI的ChatBot无法生成NDA草稿问题排查
问题:基于Python、LangChain、OpenAI及LlamaIndex的ChatBot无法生成文档草稿
我正在构建一个基于Python、LangChain、OpenAI及LlamaIndex的ChatBot,目标是输入类似“为Mike LLC与Fantasty World生成加州保密协议草稿”的提示,结合docs目录中的保密协议示例生成文档草稿,但ChatBot始终返回“无法生成草稿”的回复。我尝试添加识别“draft”关键词的逻辑,直接调用OpenAI API生成草稿,但该方案未利用到已构建的向量索引,问题仍未解决。
相关代码
向量索引创建代码
import sys import os import openai import constants import gradio as gr from langchain.chat_models import ChatOpenAI from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, LLMPredictor, PromptHelper, load_index_from_storage # Disable SSL certificate verification (for debugging purposes) os.environ['REQUESTS_CA_BUNDLE'] = '' # Set it to an empty string os.environ["OPENAI_API_KEY"] = constants.APIKEY openai.api_key = os.getenv("OPENAI_API_KEY") print(os.getenv("OPENAI_API_KEY")) def createVecorIndex(path): max_input = 4096 tokens = 512 chunk_size = 600 max_chunk_overlap = 0.1 prompt_helper = PromptHelper(max_input, tokens, max_chunk_overlap, chunk_size_limit=chunk_size) #define llm llmPredictor = LLMPredictor(llm=ChatOpenAI(temperature=.7, model_name='gpt-3.5-turbo', max_tokens=tokens)) #load data docs = SimpleDirectoryReader(path).load_data() #create vector index vectorIndex = GPTVectorStoreIndex(docs, llmpredictor=llmPredictor, prompt_helper=prompt_helper) vectorIndex.storage_context.persist(persist_dir='vectorIndex.json') return vectorIndex vectorIndex = createVecorIndex('docs')
首次查询逻辑
def chatbot(input_index): query_engine = vectorIndex.as_query_engine() response = query_engine.query(input_index) return response.response gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Super Awesome Chatbot").launch()
优化后的查询逻辑
def chatbot(input_index): query_engine = vectorIndex.as_query_engine() # If the "draft" clause is active: if "draft" in input_index.lower(): # Query the vectorIndex for relevant information/context vector_response = query_engine.query(input_index).response print(vector_response) # Use vector_response as context to query the OpenAI API for a draft prompt = f"Based on the information: '{vector_response}', generate a draft for the input: {input_index}" response = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=512, temperature=0.2 ) openai_response = response.choices[0].text.strip() return openai_response # If "draft" clause isn't active, use just the vectorIndex response else: print('else clause') return query_engine.query(input_index).response
解决方案
1. 排查向量索引查询有效性
- 验证文档加载:在
createVecorIndex函数中添加print(len(docs)),确认docs目录下的文件是否被正确读取。如果输出为0,检查路径是否正确、文件格式是否为LlamaIndex支持的类型(如txt、pdf,特殊格式需额外处理)。 - 调整文档拆分参数:当前
chunk_size=600可能过小,导致上下文碎片化。尝试将chunk_size改为1000、max_chunk_overlap改为0.2,优化文档拆分逻辑,提升检索相关性。 - 单独测试索引查询:直接运行
query_engine.query("保密协议核心条款"),查看返回结果是否包含docs中的示例内容,确认索引是否有效。
2. 优化草稿生成的Prompt与模型调用
当前Prompt未明确引导模型复用示例结构,且使用的text-davinci-002模型已停止更新,建议调整为:
if "draft" in input_index.lower(): vector_response = query_engine.query(input_index).response # 明确要求模型遵循示例格式 prompt = f""" 请参考以下保密协议示例内容: {vector_response} 严格遵循示例的条款结构和格式,为{input_index}生成符合加州法律要求的保密协议草稿,需包含保密范围、双方义务、期限、违约责任等核心条款。 """ # 使用gpt-3.5-turbo模型,采用ChatCompletion接口 response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "你是专业的法律文档生成助手,擅长根据示例生成合规的法律草稿。"}, {"role": "user", "content": prompt} ], max_tokens=1024, temperature=0.2 ) openai_response = response.choices[0].message.content.strip() return openai_response
3. 添加错误捕获与日志
在关键逻辑处添加异常捕获,定位具体问题:
def chatbot(input_index): query_engine = vectorIndex.as_query_engine() try: if "draft" in input_index.lower(): vector_response = query_engine.query(input_index).response if not vector_response: return "未检索到相关保密协议示例,请检查docs目录内容" # 后续Prompt与API调用逻辑 else: return query_engine.query(input_index).response except Exception as e: return f"生成失败:{str(e)}"
4. 复用已构建的向量索引
当前每次启动都会重新创建索引,改为优先加载已持久化的索引,避免重复构建:
from llama_index import StorageContext def get_or_create_vector_index(path): persist_dir = 'vectorIndex.json' if os.path.exists(persist_dir): storage_context = StorageContext.from_defaults(persist_dir=persist_dir) return load_index_from_storage(storage_context) else: max_input = 4096 tokens = 512 chunk_size = 1000 max_chunk_overlap = 0.2 prompt_helper = PromptHelper(max_input, tokens, max_chunk_overlap, chunk_size_limit=chunk_size) llmPredictor = LLMPredictor(llm=ChatOpenAI(temperature=.7, model_name='gpt-3.5-turbo', max_tokens=tokens)) docs = SimpleDirectoryReader(path).load_data() vectorIndex = GPTVectorStoreIndex(docs, llmpredictor=llmPredictor, prompt_helper=prompt_helper) vectorIndex.storage_context.persist(persist_dir=persist_dir) return vectorIndex vectorIndex = get_or_create_vector_index('docs')
内容的提问来源于stack exchange,提问作者Mike Mann
相关产品推荐
相关产品推荐

