赞
踩
- # -*- coding: utf-8 -*-
- import requests
- from urllib import parse
- import json
- import sys
-
- def chat_with_api(api_key, model, messages, temperature=1, top_p=1, n=1, stop=None):
- base_url = "https://api地址" # 请替换为你实际的 OpenAI API URL
-
- headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
-
- data = {
- "model": model,
- "messages": messages,
- "temperature": temperature,
- "top_p": top_p,
- "n": n,
- }
-
- if stop is not None:
- data["stop"] = stop
-
- response = requests.post(base_url, headers=headers, data=json.dumps(data))
-
- if response.status_code == 200:
- return response.json()
- else:
- raise Exception(f"Error {response.status_code}: {response.text}")
-
- def main():
- if len(sys.argv) != 5:
- print("命令行参数长度不为5")
- sys.exit()
-
- LabelCookie = parse.unquote(sys.argv[1])
- LabelUrl = parse.unquote(sys.argv[2])
- PageType = sys.argv[3]
- SerializerStr = parse.unquote(sys.argv[4])
-
- if not SerializerStr.startswith('{"'):
- with open(SerializerStr, 'r') as file_object:
- SerializerStr = file_object.read()
- SerializerStr = parse.unquote(SerializerStr)
-
- LabelArray = json.loads(SerializerStr)
-
- if PageType == "Save":
- # 这里换成你的 OpenAI API Key
- YOUR_API_KEY = "key"
-
- if LabelArray['提示词'].strip():
- prompt = LabelArray['提示词']
-
- messages = [
- {"role": "user", "content": prompt},
- ]
-
- try:
- response = chat_with_api(YOUR_API_KEY, "gpt-3.5-turbo", messages)
-
- # 获取生成的内容
- generated_content = response['choices'][0]['message']['content']
-
- # 在每个段落后添加 </br> 标签
- generated_content_with_breaks = "".join(generated_content.split('n'))
-
- LabelArray['内容'] = generated_content_with_breaks
- except Exception as e:
- print(f"Error during API call: {e}")
-
- LabelArray = json.dumps(LabelArray)
- print(LabelArray)
-
- if __name__ == "__main__":
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。