如何在LangChain的my_chain中动态访问并修改examples?
问题
假设我们已通过如下代码,基于my_schema创建了LangChain链my_chain:
from langchain.chat_models import ChatOpenAI from kor.extraction import create_extraction_chain from kor.nodes import Object, Text, Number from langchain.chat.models import ChatOpenAI from langchain.llms import OpenAI schema = Object( id="bank_statement_info", description="bank statement information about a given person.", attributes=[ Text( id="first_name", description="The first name of the person", examples=[("John Smith", "John")], ), Text( id="last_name", description="The last name of the person", examples=[("John Smith", "Smith")], ), Text( id="account_number", description="Account Number of the person in Bank statement.", examples=[("Account Number: 122-233-566-800", "122-233-566-800")], ), Text( id="address", description="address of the person in Bank statement.", ), Text( id="opening_balance", description="opening blance of the person in Bank statement.", examples=[("opening Balance: 245,800.00","245,800.00")] ), Text( id="closing_balance", description="closing blance of the person in Bank statement.", examples=[("Closing Balance: 591,800.00","591,800.00")] ), ], examples=[ ( """ya 231 Valley Farms Street FIRST Santa Monica, CA 90403 STATEMENT OF ACCOUNT CITIZENS __firstcitizensbank@domain.com BANK Account Number: 122-233-566-800 Statement Date: 11/22/2019 Page 1 of 1 Period Covered: 05/22/2019 to 11/22/2019 Eric Nam Opening Balance: 175,800.00 240 st, apt 15, hill road, Total Credit Amount: 510,000.00 Baverly Hills, LA, 90209 Total Debit Amount: 94,000.00 Closing Balance: 591,800.00 Branch - Baverly Hills Account Type: Saving Account""", [ {"first_name": "John", "last_name": "Smith", "account_number": '122-233-566-800',"address":"240 st, apt 15, hill road,Baverly Hills, LA, 90209", "Closing Balance": "458,589.00","opening Balance": "800.00"}, {"first_name": "Jane", "last_name": "Doe", "age": '923-533-256-205',"address":"850 st, apt 82, hill road,Baverly Hills, New york, 82044", "Closing Balance": "1000.00","opening Balance": "125,987.00"}, ], ) ], many=True, ) llm = ChatOpenAI(temperature=0.0, openai_api_key='', open_api_base='', model_kwargs={'engine': 'openai_gpt_4'} my_chain = create_extraction_chain(llm, my_schema, encoder_or_encoder_class='json')
我希望在创建my_chain后访问其examples,尝试过访问my_chain.prompt但无法找到对应的examples,尤其想实现动态修改my_chain的examples,请问该需求是否可行?
解决方案
可行,具体操作方式如下:
1. 访问链中的examples
创建后的my_chain会保留对原始schema的引用,你可以直接通过my_chain.schema访问整个schema结构,进而获取各级别的examples:
# 获取Object节点级别的示例 object_level_examples = my_chain.schema.examples # 获取单个属性的示例(比如first_name的示例) first_name_examples = my_chain.schema.attributes[0].examples # 获取opening_balance的示例 opening_balance_examples = my_chain.schema.attributes[4].examples
2. 动态修改examples
直接修改my_chain.schema对应的examples属性即可,修改后的示例会在后续调用链时生效:
# 修改Object级别的示例 new_object_example = ( """新的银行账单文本: Account Number: 999-888-777-666 Lily Evans Opening Balance: 200,000.00 123 Main St, New York, NY 10001 Closing Balance: 250,000.00""", [ {"first_name": "Lily", "last_name": "Evans", "account_number": "999-888-777-666", "address": "123 Main St, New York, NY 10001", "opening_balance": "200,000.00", "closing_balance": "250,000.00"} ] ) my_chain.schema.examples = [new_object_example] # 修改单个属性的示例(比如更新closing_balance的示例) my_chain.schema.attributes[5].examples = [("Closing Balance: 300,000.00", "300,000.00")]
注意事项
- 由于
create_extraction_chain会直接引用传入的schema对象,修改my_chain.schema会同时改变原始的schema对象。如果需要保留原始schema的完整性,建议先深拷贝一份再修改,之后可以基于拷贝后的schema重新创建链:
import copy # 深拷贝原始schema copied_schema = copy.deepcopy(my_chain.schema) # 修改拷贝后的schema的示例 copied_schema.examples = [new_object_example] # 基于新schema创建链 new_chain = create_extraction_chain(llm, copied_schema, encoder_or_encoder_class='json')
内容的提问来源于stack exchange,提问作者singa1994
相关产品推荐
相关产品推荐

