Преглед на файлове

这才是真确嗲api的方法和流程,网上的都是老的错的。

sszf7605 преди 9 месеца
родител
ревизия
5390d259c9
променени са 3 файла, в които са добавени 13 реда и са изтрити 18 реда
  1. 4 4
      module/ai.py
  2. 8 13
      module/openai_api.py
  3. 1 1
      requirements.txt

+ 4 - 4
module/ai.py

@@ -3,14 +3,15 @@
 
 import configparser
 import tkinter as tk
-import openai
+from openai import OpenAI
+
+client = OpenAI(api_key="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")
 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"])
 
 
@@ -19,8 +20,7 @@ def submit_text():
     # 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}])
+    completion = client.chat.completions.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')

+ 8 - 13
module/openai_api.py

@@ -30,21 +30,16 @@
 # 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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"),
-)
+client = OpenAI(api_key="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")
 
-chat_completion = client.chat.completions.create(
-    messages=[
-        {
-            "role": "user",
-            "content": "Say this is a test",
-        }
-    ],
+completion = client.chat.completions.create(
     model="gpt-3.5-turbo",
+    messages=[
+        {"role": "system", "content": "You are a helpful assistant."},
+        {"role": "user", "content": "Hello!"}
+    ]
 )
+
+print(completion.choices[0].message)

+ 1 - 1
requirements.txt

@@ -1 +1 @@
-openai==0.28
+openai