构建RAG应用遇LangChainInterface验证错误:如何配置Watsonx凭证?
问题:Watsonx LangChainInterface Pydantic验证错误解决办法
错误详情
运行代码时触发Pydantic验证错误,具体信息如下:
pydantic.error_wrappers.ValidationError: 3 validation errors for LangChainInterface credentials instance of Credentials expected (type=type_error.arbitrary_type; expected_arbitrary_type=Credentials) model_id extra fields not permitted (type=value_error.extra) project_id extra fields not permitted (type=value_error.extra) Traceback: File "D:\projects\UniGpt\APP\app.py", line 22, in <module> llm = LangChainInterface( ^^^^^^^^^^^^^^^^^^^ File "C:\Users\CHAMIKA\unigpt-env\Lib\site-packages\langchain_core\load\serializable.py", line 113, in __init__ super().__init__(*args, **kwargs) File "pydantic\main.py", line 341, in pydantic.main.BaseModel.__init__
问题代码
#import logchain dependancies from langchain.document_loaders import PyPDFLoader from langchain.indexes import VectorstoreIndexCreator from langchain.chains import RetrievalQA from langchain.embeddings import HuggingFaceEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter #bring in steamlit for UI dev import streamlit as st #bring in watsonx interface from wxai_langchain.llm import LangChainInterface, Credentials from config import API_KEY,URL,PROJECT_ID #setup cerdentials directly in the code creds={ 'api_key': API_KEY, 'url': URL, } #create llm using langchain llm = LangChainInterface( credentials=creds, model_id="granite-3-3-8b-instruct", params={ 'decoding_method':'sample', 'max_new_tokens':200, 'temprature':0.5 }, project_id=PROJECT_ID ) #function to load and index the pdf document @st.cache_resource def load_pdf(): pdf_name = "Resources/HandBook.pdf" #load the pdf loader = PyPDFLoader(pdf_name) #split the pdf into chunks index=VectorstoreIndexCreator( embedding=HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"), text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) ).from_loaders([loader]) return index #setup the retrieval qa chain chain=RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=load_pdf().vectorstore.as_retriever(), input_key="question" ) #setup the app title st.title("Ask UniExpert") #setup session state message variable to hold all the old message if "messages" not in st.session_state: st.session_state.messages = [] #display all the historical messages for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) #build a prompt input template to display the prompts prompt = st.chat_input("Enter your question:") #if the user hits enter if prompt: #display the prompt st.chat_message("user").markdown(prompt) #store the user prompt in state st.session_state.messages.append({"role": "user", "content": prompt}) #response from llm response=chain.run(prompt) #show the response st.chat_message("assistant").markdown(response) #store the response in state st.session_state.messages.append( {"role": "assistant", "content": response})
已尝试方案
- 以字典形式传递凭证
- 查找Credentials类的正确用法
解决方案
1. 正确实例化Credentials类
不能直接传字典给credentials参数,必须创建Credentials类的实例:
creds = Credentials(api_key=API_KEY, url=URL)
2. 调整model_id和project_id的传递位置
当前版本的wxai_langchain中,model_id和project_id不属于LangChainInterface的直接参数,需要放到params字典里。
3. 修正参数拼写错误
注意参数temperature的正确拼写(原代码写成了temprature)。
修改后的LLM创建代码
# 实例化Credentials creds = Credentials(api_key=API_KEY, url=URL) # 创建llm实例 llm = LangChainInterface( credentials=creds, params={ 'model_id': "granite-3-3-8b-instruct", 'project_id': PROJECT_ID, 'decoding_method':'sample', 'max_new_tokens':200, 'temperature':0.5 } )
把这段代码替换原代码中对应的部分,就能解决Pydantic验证错误。
内容的提问来源于stack exchange,提问作者Chamika Udayanga
相关产品推荐
相关产品推荐

