大模型应用开发教程

📅 2026/6/23 22:55:01
大模型应用开发教程
目录一、什么是AI大模型开发二、Gradio快速入门三、什么是提示词一、什么是AI大模型开发二、Gradio快速入门Gradio 是一个用于快速构建机器学习模型 Web 界面的 Python 库核心优势是‌仅需几行代码即可将函数转化为可交互的 UI‌。以下是从安装到进阶的核心教程。‌‌官方网站:https://www.gradio.app/官方文档 - 快速开始:https://www.gradio.app/guides/quickstart1、安装pip install gradio出现这个报错先更新pip先去到这个目录D:\Program Files\Python312\然后输入下面这个命令python.exe -m pip install --upgrade pip在PyCharm新建一个项目新建一个llm-exercise1项目新建一个part.py,代码如下案例1一个接收文本输入并返回该文本倒序输出的应用。 import gradio as gr #功能实现5 def reverse_text(text): return text[::-1] #界面配置 demo gr.Interface( fnreverse_text,# 调用reverse_text函数 inputstext, #输入组件类型为文本 outputstext) #输出组件类型为文本 #启动应用 demo.launch()在软件PyCharm的终端安装gradio因为虚拟环境没有gradio在终端输入 install gradio -i https://pypi.tuna.tsinghua.edu.cn/simple判断是否安装pip show gradio运行 part1.py在浏览器输入http://127.0.0.1:7860输入内容2、 把 PyCharm 解释器切换成全局 Python把 PyCharm 解释器切换成全局 Python不用 venvCtrlAltS→ Project:xxx → Python Interpreter顶部下拉选择不带venv路径的 python.exe应用保存直接复用 CMD 全局已安装的 cv2、gradio3、生成图片的素描的demo先安装 pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simplepip show opencv-python创建part2.pyimport gradio as gr import numpy as np import cv2 def image_to_sketch(image): gray_image image.convert(L) inverted_image 255 - np.array(gray_image) blurred cv2.GaussianBlur(inverted_image, (21, 21), 0) inverted_blurred 255 - blurred pencil_sketch cv2.divide(np.array(gray_image), inverted_blurred, scale 256.0) return pencil_sketch demo gr.Interface( fnimage_to_sketch, inputs[gr.Image(label上传图片, typepil)], outputs[gr.Image(label铅笔画)], title图像转铅笔画, description将上传的图片转为铅笔画。 ) #启动应用 demo.launch()安装openaipip install openai -i https://pypi.tuna.tsinghua.edu.cn/simplepip show openai4、python调用千问大模型先配置window的环境变量配置环境变量后要重启Pycharm软件创建python3.py,内容如下#导入必要的库 import gradio as gr import os from openai import OpenAI #从系统环境变量中获取DashScope API密钥 # 确保在运行此脚本前已设置DASHSCOPE_API_KEY环境变量 api_key os.getenv(DASHSCOPE_API_KEY) print(读取到的密钥, api_key) #如果未设置API密钥提供友好的错误提示 if not api_key: print(错误API密钥未设置。请确保在调用API之前正确设置API密钥。) # 定义一个函数用于调用通义千问max模型生成回复 def call_qwen(message, history): #调用通义千问max模型的函数 #message(str): 用户当前输入的消息内容 #history(list): 聊天历史记录支持两种格式: #格式1: [(用户消息助手回复), ...] - 元组列表形式 #格式2: [{role: user “content: 消息内容 }...]-字典列表形式 if not api_key: return 未设置DASHSCOPE_API_KEY环境变量 client OpenAI( api_keyapi_key, # 使用环境变量中的API密钥进行身份验证 base_urlhttps://dashscope.aliyuncs.com/compatible-mode/v1 # DashScope兼容模式端点 ) messages [] if history: try: #尝试处理字典格式的history(较新版本Gradio的格式) for msg in history: if isinstance(msg, dict) and role in msg and content in msg: messages.append(msg) elif isinstance(msg, (list, tuple)) and len(msg) 2: user_msg, assistant_msg msg messages.append({role: user, content: user_msg}) messages.append({role: assistant, content: assistant_msg}) except Exception as e: print(f处理历史记录时出错:{e}) messages.append({role:user,content:message}) try: response client.chat.completions.create( modelqwen-max, messagesmessages, streamFalse ) return response.choices[0].message.content except Exception as e: return Error: str(e) demogr.ChatInterface( fncall_qwen, title通义千问-max, description 基于通义千问max的聊天机器人, examples[ #指定处理聊天消息的回调函数将调用通 [你好], [你叫什么名字?], [给我讲一个笑话呗] ] ) #主程序入口点 #当直接运行此脚本时启动GradioWeb服务器 if __name__ __main__: # 启动Gradio服务默认监听本地7860端口 # 用户访问该URL即可与通义千问Turbo模型进行交互 demo.launch(themegr.themes.Soft())启动项目三、什么是提示词提示词提示工程(Prompt Engineering)是一项通过优化提示词(Prompt)和生成策略从而获得更好的模型返回结果的工程技术。好的Prompt需要不断调优。说清楚自己到底想要什么要具体!不要让机器去猜测太多。为了不让机器去猜测我们就需要告诉细节。提示工程有一些技巧灵活掌握事半功倍。提示词构成指示(Instruction):描述要让它做什么?上下文(Context):给出与任务相关的背景信息例子(Examples):给出一些例子让模型知道怎么回复输入(Input):任务的输入信息输出(OutputFormat):输出的格式想要什么形式的输出?