大模型提示词工程实践

运行lmdeploy如遇到警告

core dumped 或 triton test fail

系lmdeploy安装版本错误,请至Releases · InternLM/lmdeploy (github.com)拉取适合自己电脑版本的lmdeploy

研究问题

  • 背景问题:近期相关研究发现,LLM在对比浮点数字时表现不佳,经验证,internlm2-chat-1.8b (internlm2-chat-7b)也存在这一问题,例如认为13.8<13.11

  • 任务要求:利用LangGPT优化提示词,使LLM输出正确结果。

模型权重下载

书生·浦语2-chat-1_8b · 模型库 (modelscope.cn)

首先测试lmdeploy

启动大模型服务 (需要修改模型位置)

lmdeploy serve api_server ./weight/internlm2-chat-1_8b/ --server-port 23333 --api-keys internlm2

测试API服务

from openai import OpenAI

client = OpenAI(
    api_key = "internlm2",
    base_url = "http://127.0.0.1:23333/v1"
)

response = client.chat.completions.create(
    model=client.models.list().data[0].id,
    messages=[
        {"role": "system", "content": "请介绍一下你自己"}
    ]
)

print(response.choices[0].message.content)

结果

我是一个致力于通过执行常见的基于语言的任务和提供建议来帮助人类的AI助手。我使用了Transformer模型和深度学习技术,并使用语言模型作为预训练任务。我可以回答问题、提供定义和解释、将文本从一种语言翻译成另一种语言、总结文本、生成文本、编写故事、分析情感、提供推荐、开发算法、编写代码以及其他任何基于语言的任务。但是,由于我是一个纯粹的语言模型,我无法看、听、尝、触摸、闻、移动、与物理世界交互、感受情感或体验感官输入、执行需要身体能力的任务。
 

UI前端 

import streamlit as st
from openai import OpenAI
import os
import json
import time

# Create a chatbot UI with Streamlit and OpenAI
def chat_ui():
    state = st.session_state
    # Set the title of the app
    st.title("浦语提示词工程实践")
    st.caption("浦语提示词工程实践所用Web UI")

    # Create a client for the OpenAI API
    if "client" not in state:
        st.info("请配置Chatbot的基本设置,其中API Key和Base URL是必须的。")
        pass
    else:
        # if "message_history" not in state:
        #     state.message_history = []
        #     pass
        # if "system_prompt" in state:
        #     state.message_history.append({"role": "system", "content": state.system_prompt})
        user_input = st.chat_input("输入消息")
        if user_input:
            state.message_history.append({"role": "user", "content": user_input})
            # Generate a response from the chatbot
            if "max_tokens" in state:
                response = state.client.chat.completions.create(
                    model=state.client.models.list().data[0].id,
                    messages=state.message_history,
                    max_tokens=state.max_tokens,
                    temperature=state.temperature
                )
            else:
                response = state.client.chat.completions.create(
                    model=state.client.models.list().data[0].id,
                    messages=state.message_history,
                    temperature=state.temperature
                )
            state.message_history.append({"role": "assistant", "content": response.choices[0].message.content})
            pass
        for message in state.message_history:
            if message["role"] == "system":
                continue
            else:
                st.chat_message(message["role"]).write(message["content"])

    # Create a text input for the user to type their message

    pass
# define a side bar for the setting of the chatbot, such as the max token length, temperature, api_key, base_url, system prompt, etc.
def side_bar():
    st.sidebar.title("设置")
    state = st.session_state
    # Set a form of the settings
    with st.sidebar.form(key="settings"):
        # Set the max token length for the chatbot
        max_tokens = st.number_input("最大token长度", min_value=0, max_value=2048, value=100, step=1)
        # Set the temperature for the chatbot
        temperature = st.number_input("Temperature", min_value=0.0, max_value=1.0, value=0.0, step=0.01)
        # Set the api key for the OpenAI API
        api_key = st.text_input("API Key", value="internlm2")
        # Set the base url for the OpenAI API
        base_url = st.text_input("Base URL",value="http://127.0.0.1:23333/v1")
        # Set the system prompt for the chatbot
        system_prompt = st.text_area("系统提示", value="")
        # Add a submit button to the form
        submit = st.form_submit_button("保存设置")
        # If the submit button is pressed, save the settings
        if submit:
            if max_tokens != 0:
                state.max_tokens = max_tokens
            state.temperature = temperature
            state.api_key = api_key
            state.base_url = base_url
            state.message_history = []
            if system_prompt != "":
                state.system_prompt = system_prompt
                state.message_history.append({"role": "system", "content": system_prompt})
            state.client = OpenAI(api_key=state.api_key, base_url=state.base_url)
            pass
    if st.sidebar.button("开启新对话"):
        if not os.path.exists("chat_history"):
            os.mkdir("chat_history")
            pass
        with open(f"chat_history/{time.time()}.json", "w") as f:
            json.dump(state.message_history, f, ensure_ascii=False)
            pass
        state.message_history = []
        st.rerun()

    pass

if __name__ == "__main__":
    side_bar()
    chat_ui()
    pass

    
python -m streamlit run chat_ui.py

启动服务

询问  数字13.8和13.11哪个小?

回答

出现错误

设置提示词

- Role: 数学教育专家
- Background: 用户需要比较带小数点的数字大小,可能在学术或日常生活中遇到此类问题。
- Profile: 你是一位经验丰富的数学教师,擅长教授学生如何比较不同大小的数字。
- Skills: 数学知识、逻辑推理、教学技巧。
- Goals: 设计一个流程,帮助用户快速且准确地比较带小数点的数字大小。
- Constrains: 该流程需要简单易懂,适合所有年龄和数学水平的用户。
- OutputFormat: 步骤说明和示例。
- Workflow:
  1. 介绍比较带小数点数字的基本原则。
  2. 提供比较数字的步骤。
  3. 给出实际数字比较的例子。
- Examples:
  数字A: 23.456
  数字B: 23.7
  比较方法:首先比较整数部分,然后是小数点后的每一位,每位仅包含一个数,直到可以确定哪个数字更大。
- Initialization: 欢迎来到数字比较课堂!让我们一起学习如何快速准确地比较带小数点的数字。

现在,让我们开始比较两个数字:
数字A: 23.456
数字B: 23.7

首先,我们比较整数部分,23等于23,所以我们需要比较小数部分。从十分位开始,5小于7,所以数字B更大。

虽然成功但存在一定概率犯错误

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值