{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 读取环境变量\n", "from dotenv import load_dotenv\n", "load_dotenv() # 加载 .env 文件" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用通义千问\n", "from langchain_community.llms import Tongyi\n", "\n", "import os\n", "\n", "# 初始化通义模型(以qwen-max为例)\n", "llm_tongyi = Tongyi(\n", " model_name=\"qwen-turbo\",\n", " dashscope_api_key=os.getenv(\"DASHSCOPE_API_KEY\")\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 构造提示,告诉模型用 JSON 输出\n", "prompt = \"\"\"\n", "请根据以下用户请求,生成一个适合用于搜索的查询,并说明理由。\n", "用户请求: “{0}”\n", "\n", "请按照以下 JSON 格式返回:\n", "{{\n", " \"search_query\": \"...\",\n", " \"justification\": \"...\"\n", "}}\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# Invoke the augmented LLM\n", "output = llm_tongyi.invoke(prompt.format(\"How does Calcium CT score relate to high cholesterol?\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(output)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.3" } }, "nbformat": 4, "nbformat_minor": 4 }