使用GPT实现跨境电商智能应用

释放双眼,带上耳机,听听看~!
本文介绍如何利用GPT和Colab实现跨境电商智能应用,包括产品标题编写、销售点评估和价格范围获取。

最近在学习aigc,利用chatgpt和Colab来实现一些有趣的ai智能项目。本期就分享四个应用:跨境电商、智能客服、情感分析和图片识别

在进入这些主题之前我们需要有一个GPT账号,以及“魔法环境”。对了,我们还需要用上gpt的api,这个东西每个账号都会有5美元的api,当然,前提是这个账号是该手机号的第一个账号,像我的GPT账号就是用的国外朋友的手机号注册的,他已经注册过了,再次注册之后就没有免费的api了,所以我用的api都是身边朋友的。

一、跨境电商

假如你现在是一名跨境商人,现在给到你的任务是去美国售卖儿童玩具,你觉得你现在需要什么信息呢?

考虑产品:工厂现货遥控汽车儿童玩具
1.20个单词内用英语编写亚马逊上使用的人类可读的产品标题
2.为亚马逊中的产品写下5个卖点。
3.评估该产品在美国的价格范围
输出结果为JSON格式,具有三个名为标题、销售点和价格范围的属性
英文:
Consideration product:工厂现货遥控汽车儿童玩具

1. Compose human readable product title used on Amazon in english within 20 words
2. Write 5 selling points for the products in Amazon 
3. Evaluat a price range for this product in U.S

Output the result in json format with three properties called title,
selling_points and price_range

gpt有用全网最全的知识库,你肯定需要知道儿童玩具这个商品在美国的市场价格,他可以帮你获取各种信息,他也可以帮你写文案。当然上面的内容你都需要翻译成英文给到GPT,最后返回的json格式我这里需要说一下。

json格式基于js语言,json采用键值对的方式来组织数据,这种格式便于我们阅读和编写,便于web开发和api通信

于是GPT给出下面的信息

最近GPT被黑了啊,但是还好IOS手机端可以用

使用GPT实现跨境电商智能应用

就这样,GPT就帮我们获取了我们需要的信息。

同样,我们还可以用Colab来实现获取这些信息,colab你可以理解为一个线上ptyhon编译器

进入网站:colab.research.google.com/

点击左上角文件—新建笔记本

于是输入下面代码

!pip install openai
// 输出结果:
Collecting openai==0.10.2
  Downloading openai-0.10.2.tar.gz (156 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 156.7/156.7 kB 3.2 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai==0.10.2) (2.31.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai==0.10.2) (4.66.1)
Requirement already satisfied: pandas>=1.2.3 in /usr/local/lib/python3.10/dist-packages (from openai==0.10.2) (1.5.3)
Requirement already satisfied: pandas-stubs>=1.1.0.11 in /usr/local/lib/python3.10/dist-packages (from openai==0.10.2) (1.5.3.230304)
Requirement already satisfied: openpyxl>=3.0.7 in /usr/local/lib/python3.10/dist-packages (from openai==0.10.2) (3.1.2)
Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.10/dist-packages (from openpyxl>=3.0.7->openai==0.10.2) (1.1.0)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.2.3->openai==0.10.2) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.2.3->openai==0.10.2) (2023.3.post1)
Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.2.3->openai==0.10.2) (1.23.5)
Requirement already satisfied: types-pytz>=2022.1.1 in /usr/local/lib/python3.10/dist-packages (from pandas-stubs>=1.1.0.11->openai==0.10.2) (2023.3.1.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.10.2) (3.3.1)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.10.2) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.10.2) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.10.2) (2023.7.22)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.1->pandas>=1.2.3->openai==0.10.2) (1.16.0)
Building wheels for collected packages: openai
  Building wheel for openai (setup.py) ... done
  Created wheel for openai: filename=openai-0.10.2-py3-none-any.whl size=168323 sha256=bbc4be015b4b2df14978fe5b06c41e2a7cbc863933e43f89addb76bafb50aa98
  Stored in directory: /root/.cache/pip/wheels/70/39/75/a154ffba6d11ba573b4687fd85b5fab8700cfe0a28dc4aad5a
Successfully built openai
Installing collected packages: openai
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
llmx 0.0.15a0 requires cohere, which is not installed.
llmx 0.0.15a0 requires tiktoken, which is not installed.
Successfully installed openai-0.10.2

这里意思是安装openai这个模型

于是再输入下面代码

import openai
openai.api_key = "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"  # openai 的sdk
COMPLETION_MODEL = "text-davinci-003"  # 模型常量 达芬奇  会自动完成
# 字符串模板
# 换行
# 描述细节需求
# 分布去执行
# 输出的格式
prompt = """
Consideration product:工厂现货PVC充气青蛙夜市地摊充气玩具发光蛙儿童水上玩具

1. Compose human readable product title used
on Amazon in english within 20 words
2. Write 5 selling points for the products in Amazon
3. Evaluat a price range for the products in U.S

Output the result in json format with three properties called title
selling_points and price_range
"""

# 定义函数  调用接口
def get_response(prompt):
    # 远程发送请求
    completions = openai.Completion.create(
        # 大模型是很值钱的
        engine = COMPLETION_MODEL,  # 模型
        prompt = prompt,
        max_tokens = 512,   # 这里可以让我们省点钱
        n = 1,   # 返回一条结果
        stop = None,
        temperature = 0.0   # 自由发挥度 0-2 之间
    )

    # print(comletions)
    message = completions.choices[0].text #
    return message

print(get_response(prompt))

上面代码中api_key是需要我们进入openai网站中登录之后进入API这个选项,里面自己新建一个api,命名之后可以获得该api地址了。

prompt的是提示词的意思,把我们刚刚的需求放进来,让ai帮我们去获得我们想要的信息。对了prompt engineer提示词工程师是一个新兴的岗位,最近非常火爆。

输出结果:

{
    "title": "Factory Stock PVC Inflatable Frog Night Market Stall Inflatable Toy Glowing Frog Water Toy for Kids",
    "selling_points": [
        "Made of durable PVC material",
        "Inflatable design for easy storage and transport",
        "Glow in the dark feature for added fun",
        "Perfect for pool parties and beach trips",
        "Great gift for kids"
    ],
    "price_range": "$10 - $20"
}

利用这个方法给出的结果会比GPT给出的信息更为精准

二、智能客服

同样的,客服我们也可以交给AI来做

!pip install openai==0.10.2

这里的数字是openai大模型的版本号

同样的,需求我们只需要放进prompt中即可

# 基于 openai的大模型来强化客服能力
import openai
openai.api_key="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"
# 常量 文本生成的模型
COMPLETION_MODEL="text-davinci-003"
# 系统后台生成了一条记录,再调用我们这个AIGC程序生成客服
prompt="""请你使用朋友的语气回复到客户,并称他为亲,他的订单已经发货在路上了,预计在3天之内会送达,订单号2021AEDG,我们很抱歉,物流时间比原来长,感谢他选购我们的商品。"""
# 封装了openai 回复的功能 越高越智能
def get_response(prompt, temperature=1.0):
  # Completion 模块
  # 调用openai库的completion模块,让其创建一个新的
  # 字典{Key:value}
  completions = openai.Completion.create(
      engine=COMPLETION_MODEL,
      prompt=prompt,
      max_tokens=1024,
      n=1,
      # Nonde相当于null
      stop=None,
      temperature=temperature
  )
  # JSON choices txt
  print(completions)
  message = completions.choices[0].text
  return message

运行结果:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "nnu4eb2uff01u60a8u7684u8ba2u53552021AEDGu5b89u6392u53d1u8d27u6210u529fu5566uff0cu9884u8ba1u57283u5929u4e4bu5185u4f1au9001u8fbeuff01u5f88u62b1u6b49uff0cu7269u6d41u65f6u95f4u6bd4u539fu6765u9884u8ba1u957fuff0cu4f46u662fu8fd8u662fu6709u4fddu969cu60a8u7684u5546u54c1u4f1au5728u89c4u5b9au65f6u95f4u6b63u5e38u6536u5230uff0cu611fu8c22u60a8u9009u8d2du6211u4eecu7684u5546u54c1uff01"
    }
  ],
  "created": 1699360595,
  "id": "cmpl-8IFbn528d124ZkSXY1f931UUlulgV",
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 174,
    "prompt_tokens": 168,
    "total_tokens": 342
  },
  "warning": "This model version is deprecated. Migrate before January 4, 2024 to avoid disruption of service. Learn more https://platform.openai.com/docs/deprecations"
}


亲!您的订单2021AEDG安排发货成功啦,预计在3天之内会送达!很抱歉,物流时间比原来预计长,但是还是有保障您的商品会在规定时间正常收到,感谢您选购我们的商品!

三、情感分析

同样的,情感分析我们也可以交给ai来做,我们只需要给出一句话,它就可以分析出我们的情绪

我们在colab中新建一个文件,安装一个transformer,这个模型来自huggingface抱抱脸

!pip install transformers

输入下面代码派发一个情感分析模块

from transformers import pipeline # pipeline 派发
classifer = pipeline('sentiment-analysis') # 情感分析

出现一系列的绿条就代表你已经安装成功

现在我们可以试试效果

result = classifer('thank you')
result // [{'label': 'POSITIVE', 'score': 0.9998352527618408}]

没有问题

如果我们想要让他读懂中文,我们需要给他安装一个中文模型

classifer = pipeline('sentiment-analysis',
  model='uer/roberta-base-finetuned-dianping-chinese')

同样,当出现绿条代表你已经安装成功

现在我们试试效果

result = classifer('好玩,爱玩')
result // [{'label': 'positive (stars 4 and 5)', 'score': 0.8053399920463562}]

也没有问题

四、图片识别

先安装一个transformer

!pip install transformers

我们也可以看看transformers能派发(pipeline)哪些任务

from transformers.pipelines import SUPPORTED_TASKS
SUPPORTED_TASKS

你也可以让他以一个“字典”的形式输出出来

print(SUPPORTED_TASKS.items())

你也可以将其遍历出来

for k, v in SUPPORTED_TASKS.items():
  print(k)

好,现在安装一个图片识别模型

from transformers import pipeline
checkpoint = "google/owlvit-base-patch32" # 图片识别模型
detector = pipeline('zero-shot-object-detection',model=checkpoint)

等待绿条出现,安装成功

现在我们可以试试效果,将一个图片的地址放进去识别

import requests # python 的 http 请求
from PIL import Image
url = "https://unsplash.com/photos/oj0zeY2Ltk4/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MTR8fHBpY25pY3xlbnwwfHx8fDE2Nzc0OTE1NDk&force=true&w=640"
img = Image.open(requests.get(url, stream=True).raw)
img

图片样例

使用GPT实现跨境电商智能应用
现在我们要输出下面需要识别的目标,并输出其坐标

predictions = detector(
    img,
    candidate_labels= ["hat", "sunglasses", "book", "fruit"]
)
predictions

输出结果

[{'score': 0.225620299577713,  'label': 'sunglasses',  'box': {'xmin': 349, 'ymin': 228, 'xmax': 429, 'ymax': 265}}, {'score': 0.15738625824451447,  'label': 'book',  'box': {'xmin': 270, 'ymin': 284, 'xmax': 503, 'ymax': 427}}, {'score': 0.10808201134204865,  'label': 'hat',  'box': {'xmin': 38, 'ymin': 172, 'xmax': 260, 'ymax': 363}}]

对目标元素进行绘制

from PIL import ImageDraw # PIL是python图片库 写模块
draw = ImageDraw.Draw(img)
for prediction in predictions:
  box = prediction["box"]
  label = prediction["label"]
  score = prediction["score"]
  xmin, ymin, xmax, ymax = box.values() # 结构
  draw.rectangle((xmin, ymin, xmax, ymax),outline="red",width=1)
  draw.text((xmin,ymin), f"{label}: {round(score, 2)}", fill="red")

img

PIL是python的图片库

输出结果

使用GPT实现跨境电商智能应用

绘制成功!

如果觉得本文对你有帮助的话,可以给个免费的赞吗[doge] 还有可以给我的gitee链接codeSpace: 记录coding中的点点滴滴 (gitee.com)点一个免费的star吗[星星眼]

本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
AI教程

Copilot源码详细分析

2023-11-20 18:59:55

AI教程

GPT4 vs Claude2: 详细对比

2023-11-20 19:03:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索