浏览代码

测试openai api

gongyuan 9 月之前
父节点
当前提交
5e135f7874
共有 3 个文件被更改,包括 102 次插入0 次删除
  1. 51 0
      module/ai.py
  2. 50 0
      module/openai_api.py
  3. 1 0
      requirements.txt

+ 51 - 0
module/ai.py

@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+# coding: utf-8
+
+import configparser
+import tkinter as tk
+import openai
+import threading
+#
+# config = configparser.ConfigParser()
+# config.read("config.txt")
+
+# supply your API key however you choose
+openai.api_key = "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"
+# print(config['example']["key"])
+
+
+def submit_text():
+    # Get the content of the text box and print it
+    # Get text without the newline character
+    text = text_input.get("1.0", "end-1c")
+    text_doing.insert(tk.END, "莫着急,正在等待网页响应中。。。")
+    completion = openai.ChatCompletion.create(
+        model="gpt-3.5-turbo", messages=[{"role": "user", "content": text}])
+    print(completion.choices[0].message.content)
+    text_doing.delete("1.0", 'end')
+    text_output.insert(tk.END, completion.choices[0].message.content + '\n')
+
+
+def start_submit_text():
+    threading.Thread(target=submit_text).start()
+
+
+# Create the main windo
+root = tk.Tk()
+
+# Create a text box
+text_input = tk.Text(root, height=20, width=100, font=(16))
+text_input.pack()
+
+text_output = tk.Text(root, height=20, width=100, font=(16))
+text_output.pack()
+text_doing = tk.Text(root, height=1, width=30, font=(16))
+text_doing.place(x=100, y=100)
+text_doing.pack()
+# Create a button
+submit_button = tk.Button(
+    root, text="提交", command=start_submit_text, height=5, width=50)
+submit_button.pack()
+
+# Start the main loop
+root.mainloop()

+ 50 - 0
module/openai_api.py

@@ -0,0 +1,50 @@
+# import openai
+# openai.api_key = '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'
+# # 通过 `系统(system)` 角色给 `助手(assistant)` 角色赋予一个人设
+# messages = [{'role': 'system', 'content': '你是一个乐于助人的诗人。'}]
+# # 在 messages 中加入 `用户(user)` 角色提出第 1 个问题
+# messages.append({'role': 'user', 'content': '作一首诗,要有风、要有肉,要有火锅、要有雾,要有美女、要有驴!'})
+# # 调用 API 接口
+# response = openai.ChatCompletion.create(
+#     model='gpt-3.5-turbo',
+#     messages=messages,
+# )
+# # 在 messages 中加入 `助手(assistant)` 的回答
+# messages.append({
+#     'role': response['choices'][0]['message']['role'],
+#     'content': response['choices'][0]['message']['content'],
+# })
+# # 在 messages 中加入 `用户(user)` 角色提出第 2 个问题
+# messages.append({'role': 'user', 'content': '好诗!好诗!'})
+# # 调用 API 接口
+# response = openai.ChatCompletion.create(
+#     model='gpt-3.5-turbo',
+#     messages=messages,
+# )
+# # 在 messages 中加入 `助手(assistant)` 的回答
+# messages.append({
+#     'role': response['choices'][0]['message']['role'],
+#     'content': response['choices'][0]['message']['content'],
+# })
+# # 查看整个对话
+# print(messages)
+
+
+import os
+from openai import OpenAI
+
+client = OpenAI(
+    # This is the default and can be omitted
+    api_key=os.environ.get(
+        "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"),
+)
+
+chat_completion = client.chat.completions.create(
+    messages=[
+        {
+            "role": "user",
+            "content": "Say this is a test",
+        }
+    ],
+    model="gpt-3.5-turbo",
+)

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+openai==0.28