智能简历平台Plus——AI润色功能实现(8)

概要

  • 前端实现
  • 后端API的接入
  • 页面演示

前端

代码思路
发送请求:

  1. axios.post(‘http://localhost:8000/api/generate_response’, {
    user_input: this.evaluationForm.content }): 向
    http://localhost:8000/api/generate_response 发送一个 POST 请求。 请求负载是一个包含
    user_input 属性的对象,值为 this.evaluationForm.content。 处理响应:

  2. .then(response => {…}): 当请求成功时执行此代码块。 response.data 包含了 API 返回的数据。
    console.log(‘API响应数据:’, response.data):输出响应数据到控制台。 if (response.data
    && response.data.message) {…}:检查响应数据中是否包含 message 属性。

  3. this.responseMessage = response.data.message:如果包含 message 属性,将其赋值给
    this.responseMessage。 console.log(‘设置的responseMessage:’,
    this.responseMessage):输出设置的响应消息到控制台。 else
    {…}:如果响应数据格式不正确,输出错误信息到控制台。

  4. 处理错误:

    .catch(error => {…}): 当请求失败时执行此代码块。 console.error(‘生成回复时出错:’,
    error):输出错误信息到控制台。

 generateResponse() {
      axios.post('http://localhost:8000/api/generate_response', { user_input: this.evaluationForm.content })
        .then(response => {
          console.log('API响应数据:', response.data); // 输出API响应数据以进行调试
          
          if (response.data && response.data.message) {
            this.responseMessage = response.data.message; 
            console.log('设置的responseMessage:', this.responseMessage); // 输出设置的响应消息
          } else {
            console.error('响应数据格式不正确:', response.data);
          }
        })
        .catch(error => {
          console.error('生成回复时出错:', error);
        });
    },

后端

代码思路

  1. 导入模块:
    import json:导入用于处理 JSON 数据的模块。 import requests:导入用于发送 HTTP 请求的模块。
  2. 生成请求负载:
    payload = json.dumps({…}): 使用 json.dumps 将一个字典转换为 JSON 字符串。 字典包含一个
    messages 列表,列表中的字典表示用户的输入。
  3. 发送 POST 请求: response = requests.post(url, headers=headers,
    data=payload): 使用 requests.post 方法发送 POST 请求。 url 是请求的目标 URL。 headers 包含请求头信息。 data 是请求的数据负载。
  4. 记录请求和响应:
    print(f"Requested payload: {payload}“):输出发送的请求负载到控制台进行调试。 print(f"API response: {response.text}”):输出 API 返回的响应数据到控制台进行调试。
  5. 解析响应数据:
    response_json = json.loads(response.text):将响应文本转换为 JSON 格式。 if “result” in response_json:: 检查解析后的 JSON 数据是否包含 result 字段。 如果包含,返回
    result 字段的内容。 else:: 如果不包含 result 字段,输出错误信息并返回预设的错误消息 “您的输入不符合要求”。

在之前基础上添加了与前端响应的部分

def get_response(user_input, url, headers):
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": user_input
            }
        ]
    })
    response = requests.post(url, headers=headers, data=payload)

    # 记录外部 API 请求和响应
    print(f"Requested payload: {payload}")  # Debug log
    print(f"API response: {response.text}")  # Debug log

    # 将响应文本解析为 JSON 格式
    response_json = json.loads(response.text)

    # 获取 "result" 字段的内容
    if "result" in response_json:
        return response_json["result"]
    else:
        print("您的输入不符合要求")
        return "您的输入不符合要求"

展示

后端
在这里插入图片描述
前端
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值