当前位置:   article > 正文

prompt设计之调用qwem,internlm,chatglm等模型代码方法(anaconda)_internlm2 rag prompt设计

internlm2 rag prompt设计

prompt调用模型实现效果

prompt设计之anaconda模型调用

利用模型进行prompt设计主要思路是通过魔搭社区对应模型访问,实现本地设计prompt的功能,实现效果展示:
internlm模型:
在这里插入图片描述
chatglm模型:
在这里插入图片描述
qwen模型:
在这里插入图片描述

利用魔搭社区现有的交互界面

打开浏览器搜索魔搭社区或者利用网址https://modelscope.cn/search?search=internlm进入,在搜索框搜索nternlm,然后点击模型进入,如下:

在这里插入图片描述

进入交互界面后的图形如下:

在这里插入图片描述

在这里你可以实现和模型在线聊天功能,如果要实现代码功能需要点击红色框框部分use via api

在这里插入图片描述

这样就能得到api的调用地址https://modelscope.cn/api/v1/studio/AI-ModelScope/Internlm-chat-7b/gradio/

通过anaconda实现本地访问大模型

安装python相关库

anaconda prompt里面执行以下命令:
pip install gradio_client

安装执行时所需依赖

建议先创建python虚拟环境,创建和运行:

conda create -n xx python ==3.11 #创建python虚拟环境xx,python版本设置为3.11
conda activate xx    # 激活python虚拟环境xx
  • 1
  • 2

在anaconda的jupyter实现代码

需要新建gradio_client.ipynb(对名字可能有严格要求)的源文件,然后在jupyter里面打开该文件

在这里插入图片描述

导入所用依赖

from gradio_client import Client

import json

函数初始化

定义一个名字为call_internlm_chat7b函数,用来完成模型的调用,模型输入为input_,输出为模型对于输入的输出

def call_internlm_chat7b(input_):
    """
    input_: 当前输入
    return: 当前输出
    """
    client = Client("https://modelscope.cn/api/vl/studio/AI-ModelScope/Internlm-chat-7b/gradio/")
    result = client.predict(input_,fn_index=0)
    with open(result[1],'r',encoding='utf-8') as f:
        data = json.load(f)
    return data[-1][-1]  # 返回data变量的最后一个元素的最后一个元素
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

初步体验

call_internlm_chat7b('你好')

在这里插入图片描述

这样你就可以设计属于自己的prompt了

完整代码如下:

from gradio_client import Client
import json
def call_internlm_chat7b(input_):
    """
    input_: 当前输入
    return: 当前输出
    """
    client = Client("https://modelscope.cn/api/v1/studio/AI-ModelScope/Internlm-chat-7b/gradio/")
    result = client.predict(input_, fn_index=0)
    with open(result[1], 'r', encoding='utf-8') as f:
        data = json.load(f)
    return data[-1][-1]

# 使用函数与ChatGLM6B模型进行交互
response = call_internlm_chat7b("你好")
print(response)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/389861
推荐阅读
相关标签
  

闽ICP备14008679号