1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import os from langchain_deepseek import ChatDeepSeek from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser
from dotenv import load_dotenv load_dotenv()
llm = ChatDeepSeek( model="deepseek-chat", temperature=0 )
prompt_extract = ChatPromptTemplate.from_template( "请从以下文本中提取技术规格:\n\n{text_input}" )
prompt_transform = ChatPromptTemplate.from_template( "请将以下规格转换为 JSON 对象,使用'cpu'、'memory' 和 'storage' 作为键:\n\n{specifications}" )
extraction_chain = prompt_extract | llm | StrOutputParser()
full_chain = ( { "specifications": extraction_chain } | prompt_transform | llm | StrOutputParser() )
input_text = "新款笔记本电脑配备了3.5 GHz 八核处理器,16GB 内存和1TB NVMe SSD存储。"
final_result = full_chain.invoke({ "text_input": input_text })
print("\n--- 最终 JSON 输出---") print(final_result)
|