当前位置: 首页 > article >正文

《生成式 AI》课程 第3講 CODE TASK执行文章摘要的机器人

课程

《生成式 AI》课程 第3講:訓練不了人工智慧嗎?你可以訓練你自己-CSDN博客

任务1:总结
1.我们希望你创建一个可以执行文章摘要的机器人。
2.设计一个提示符,使语言模型能够对文章进行总结。

 model: gpt-4o-mini',#'gpt-3.5-turbo',

import requests
import gradio as gr
import json


def get_response(input_text):
    url = "https://openai.api2d.net/v1/chat/completions"
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer f'  # <-- 把 fkxxxxx 替换成你自己的 Forward Key,注意前面的 Bearer 要保留,并且和 Key 中间有一个空格。
    }
    data = {
        'model': 'gpt-4o-mini',#'gpt-3.5-turbo',
        'messages': [{'role': 'user', 'content': f"对输入内容总结:{input_text}"}]
    }
    response = requests.post(url, headers=headers, json=data)
    status_code = response.status_code
    try:
        json_data = response.json()
        # 提取模型名称
        model_name = json_data.get('model', '未知模型')
        # 提取助手回复的内容
        assistant_content = json_data.get('choices', [])[0].get('message', {}).get('content', '无回复内容')
        # 提取各类token数量
        prompt_tokens = json_data.get('usage', {}).get('prompt_tokens', 0)
        completion_tokens = json_data.get('usage', {}).get('completion_tokens', 0)
        total_tokens = json_data.get('usage', {}).get('total_tokens', 0)
        return status_code, f"模型: {model_name}\n回复内容: {assistant_content}\n提示词token数: {prompt_tokens}\n回复内容token数: {completion_tokens}\n总token数: {total_tokens}"
    except json.JSONDecodeError:
        return status_code, "解析JSON出错"


iface = gr.Interface(
    fn=get_response,
    inputs=gr.Textbox(lines=2, placeholder="请输入你想发送的内容"),
    outputs=[gr.Textbox(label="状态码"), gr.Textbox(label="解析后的响应内容")]
)

iface.launch()

执行的结果


http://www.kler.cn/a/402190.html

相关文章:

  • 联通光猫(烽火通信设备)改桥接教程
  • 共建智能软件开发联合实验室,怿星科技助力东风柳汽加速智能化技术创新
  • 【学习心得】算力云平台上的大模型部署并实现远程调用
  • RabbitMQ消息可靠性保证机制4--消费端限流
  • EtherNet/IP从站转ModbusTCP主网关是一款 ETHERNET/IP 从站功能的通讯网关
  • python蓝桥杯刷题2
  • 【论文笔记】LoFLAT: Local Feature Matching using Focused Linear Attention Transformer
  • 基于AIRTEST和Jmeter、Postman的自动化测试框架
  • macbook外接2k/1080p显示器调试经验
  • linux 驱动之input子系统初探
  • unity 中 RectTransform 的常用几个属性
  • 一、Python基础语法(有C语言基础速成版)
  • 【linux】使用minicom调试串口
  • CH06_Lambda表达式
  • 【工具推荐】MobaXterm远程终端管理工具最全攻略,涉及下载、安装、字体配置、中文汉化版、中文显示乱码和中文输入乱码、adb tab无效无法补全、Telnet/ssh使用说明、使用技巧等保姆级教程
  • 驱动开发系列13 - Linux tasklet用法介绍
  • django从入门到实战(二)——FBV视图介绍
  • java实现生成PDF文件
  • 函数指针数组,转移表
  • for循环中批量调用接口,等接口全部执行完成后处理——js基础积累