使用langchain调用千问大模型并使用提示词模板的demo

LCEL模板

from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

model_name = 'qwen3-vl-plus'
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"  # 本地模型用:http://localhost:6006/v1

api_key = 'xxxxx'  # 本地模型用:非空字符串
extra_body = {"enable_thinking": True"thinking_budget": 81920}

llm = ChatOpenAI(
    model=model_name,
    api_key=api_key,
    base_url=base_url,
    temperature=0.8,
    extra_body=extra_body,
)

messages = [{"role": "system", "content": "你是一个中医经方专家"}, {"role": "human", "content": "太阳病如何辩证"}]

template_lcel = PromptTemplate.from_template("帮我生成一段关于{topic}的文案,100字左右。")
prompt = template_lcel.invoke({"topic": "桂枝汤"})

chain = template_lcel | llm
resp = chain.invoke({"topic": "桂枝汤"})
print(11111, resp.content)

ICL模板1

from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain_openai import ChatOpenAI

model_name = 'qwen3-vl-plus'
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"  # 本地模型用:http://localhost:6006/v1

api_key = 'sk-cbe6d658ada24abbbb8abc2333fcc1ba'  # 本地模型用:非空字符串
extra_body = {"enable_thinking": True, "thinking_budget": 81920}

llm = ChatOpenAI(
    model=model_name,
    api_key=api_key,
    base_url=base_url,
    temperature=0.8,
    extra_body=extra_body,
)

examples =[
        {
            "question": "穆罕默德·阿里和艾伦·图灵谁活得更久?",
            "answer": """
            是否需要后续问题:是。
            后续问题:穆罕默德·阿里去世时多大?
            中间答案:穆罕默德·阿里去世时74岁。
            后续问题:艾伦·图灵去世时多大?
            中间答案:艾伦·图灵去世时41岁
            所以最终答案是:穆罕默德·阿里
            """},
        {
        "question":"乔演·华盛顿的外祖父是谁?",
        "answer": """
        是否需要后续问题:是。
        后续问题:乔治·华盛顿的母亲是谁?
        中间答案:乔治·华盛顿的母亲是玛丽·鲍尔·华盛顿。
        后续问题:玛丽·鲍尔·华盛顿的父亲是谁?
        中间答案:玛丽·鲍尔·华盛顿的父亲是约瑟夫·鲍尔。
        所以最终答案是:约瑟夫·鲍尔
        """}
    ]
base_template = PromptTemplate.from_template("问题:{question}\n{answer}")
icl_template =FewShotPromptTemplate(
    examples=examples, #传入示例列表
    example_prompt=base_template,
    suffix="问题:{input}",
    input_variables=["input"]
)

chain = icl_template | llm

resp = chain.invoke({"input": "桂枝汤和麻黄汤的组成有哪些不同?"})
print(11111, resp.content)

ICL模板2

from langchain_core.messages import HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI

model_name = 'qwen3-vl-plus'
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"  # 本地模型用:http://localhost:6006/v1

api_key = 'sk-cbe6d658ada24abbbb8abc2333fcc1ba'  # 本地模型用:非空字符串
extra_body = {"enable_thinking": True, "thinking_budget": 81920}

llm = ChatOpenAI(
    model=model_name,
    api_key=api_key,
    base_url=base_url,
    temperature=0.8,
    extra_body=extra_body,
)

examples = [
        {"input": "1@@3等于几?", "output": "3"},
        {"input": "2@@4等于几?", "output": "8"},
        {"input": "6@@7等于几?", "output": "42"},
    ]

example_prompt = ChatPromptTemplate.from_messages([('human', '{input}'), ('ai', '{output}')])
few_shot_prompt =FewShotChatMessagePromptTemplate(examples=examples, example_prompt=example_prompt)

final_template = ChatPromptTemplate.from_messages([
("system", "你是智能机器人AI助手!"),
    few_shot_prompt,
    MessagesPlaceholder("msg")
])
chain = final_template | llm | StrOutputParser()
resp = chain.invoke({"msg": [HumanMessage(content="6@@8等于几?")]})
print(11111, resp)
Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐