惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
W
WeLiveSecurity
P
Proofpoint News Feed
月光博客
月光博客
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
Webroot Blog
Webroot Blog
T
Threatpost
Y
Y Combinator Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Vercel News
Vercel News
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
Recent Announcements
Recent Announcements
S
Security @ Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
T
The Exploit Database - CXSecurity.com
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
雷峰网
雷峰网
量子位
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
I
Intezer
T
The Blog of Author Tim Ferriss
G
GRAHAM CLULEY
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
罗磊的独立博客

ike‘s blog

mac软件推荐 用Siri来控制Windows电脑开机关机吧 clickhouse的一些优化建议和工具 2023sumppary 记录一下我的第一个网页游戏Mazeball 如何使用Clickhouse的索引 如何从zookeeper切换为clickhouse—keeper cloki分布式查询和clickhouse副本存储 使用ilogtail+cloki+clickhouse来做日志系统吧 clickhouse集群部署指南 快速安装部署clickhouse和cloki 快速部署thanos架构 如何使用go的jwt 如何修改git commit记录 业务容器化需要注意的一些地方 游戏企划 Prometheus Metrics精简优化2 来在线听音乐吧 简单搭建国内也可以使用的chatgpt Prometheus Metrics精简优化 Kubernetes的探针机制 本地部署chatAI【chatglm】 AI绘画工具webui简单入门 之 高清化 写在地下城邂逅Ⅳ·灾厄篇·完结之后 如何部署gitalk作为评论系统 AI绘画工具webui简单入门 之 工具安装 grafanadb迁移到mysql kubectl常用命令 开始写博客啦 隐私政策URL Markdown Example Include Video in the Posts Markdown Extended Features Simple Guides for Fuwari Golang net/http & HTTP Serve 源码分析
Chatgpt的API入门
ike · 2023-04-18 · via ike‘s blog

chatgpt.py

import openai
from PIL import Image
import requests

class AI:
    def __init__(self):
        # 可以不需要指定api_base,用默认的就好了,前提是网络通,默认需要翻墙
        self.api_base = "https://XXX/api/v1"
        self.api_key = "sk-XXX"
        
    def chat(self,prompt,msg):
        openai.api_base = self.api_base
        openai.api_key = self.api_key
        chat_completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages = [
            # 系统消息,它有助于设置助手的行为
            {"role": "system", "content": prompt},
            # 用户消息,输入你的问题吧
            {"role": "user", "content": msg},
            # 我们还可以添加以前的对话,但你需要用while true循环把之前的消息也发送过去,很费token
            # {"role": "assistant", "content": messages},
        ],
        )
        response = chat_completion.choices[0].message.content
        print(response)
        return 

    def image(self,prompt):
        openai.api_base = self.api_base
        openai.api_key = self.api_key
        image_completion = openai.Image.create(
            #提示要生成什么样的图片
            prompt=prompt,
            #数量
            n=1,
            #大小
            size="512x512"
        )
        image_url = image_completion['data'][0]['url']
        r = requests.get(image_url)
        with open('test.png', 'wb') as f:
            f.write(r.content)
            img = Image.open('test.png')
            img.show()
        return 

main.py

from service.chatgpt import AI
import json

def main():
    with open('data.json', "r", encoding="utf-8") as f:
        msg = json.dumps(json.load(f))
        prompt = "你现在是一个告警AI机器人,我会发送过去24小时的告警数据统计信息给你,请生成日报,要有时间范围、告警总数、TOP5告警,告警级别、告警分析、告警建议"
        ai = AI()
        ai.chat(prompt,msg)
    
    
if __name__ == "__main__":
    main()

收费:官方是 0.002 美刀/1000token,但是使用api必须把账号升级为付费账号,也就说要绑定国外的信用卡,很麻烦,目前只能用depay这种虚拟信用卡充USD来实现,或者用国内的一些代理之类的了,希望以后有更方便的方法吧。

参考官方API文档