当前位置:   article > 正文

火车头采集器python CHATGPT/AI openai改写创作/批量原创插件代码使用教程!_火车头采集器ai改写插件

火车头采集器ai改写插件
  1. # -*- coding: utf-8 -*-
  2. import requests
  3. from urllib import parse
  4. import json
  5. import sys
  6. def chat_with_api(api_key, model, messages, temperature=1, top_p=1, n=1, stop=None):
  7. base_url = "https://api地址" # 请替换为你实际的 OpenAI API URL
  8. headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
  9. data = {
  10. "model": model,
  11. "messages": messages,
  12. "temperature": temperature,
  13. "top_p": top_p,
  14. "n": n,
  15. }
  16. if stop is not None:
  17. data["stop"] = stop
  18. response = requests.post(base_url, headers=headers, data=json.dumps(data))
  19. if response.status_code == 200:
  20. return response.json()
  21. else:
  22. raise Exception(f"Error {response.status_code}: {response.text}")
  23. def main():
  24. if len(sys.argv) != 5:
  25. print("命令行参数长度不为5")
  26. sys.exit()
  27. LabelCookie = parse.unquote(sys.argv[1])
  28. LabelUrl = parse.unquote(sys.argv[2])
  29. PageType = sys.argv[3]
  30. SerializerStr = parse.unquote(sys.argv[4])
  31. if not SerializerStr.startswith('{"'):
  32. with open(SerializerStr, 'r') as file_object:
  33. SerializerStr = file_object.read()
  34. SerializerStr = parse.unquote(SerializerStr)
  35. LabelArray = json.loads(SerializerStr)
  36. if PageType == "Save":
  37. # 这里换成你的 OpenAI API Key
  38. YOUR_API_KEY = "key"
  39. if LabelArray['提示词'].strip():
  40. prompt = LabelArray['提示词']
  41. messages = [
  42. {"role": "user", "content": prompt},
  43. ]
  44. try:
  45. response = chat_with_api(YOUR_API_KEY, "gpt-3.5-turbo", messages)
  46. # 获取生成的内容
  47. generated_content = response['choices'][0]['message']['content']
  48. # 在每个段落后添加 </br> 标签
  49. generated_content_with_breaks = "".join(generated_content.split('n'))
  50. LabelArray['内容'] = generated_content_with_breaks
  51. except Exception as e:
  52. print(f"Error during API call: {e}")
  53. LabelArray = json.dumps(LabelArray)
  54. print(LabelArray)
  55. if __name__ == "__main__":
  56. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/87389
推荐阅读
相关标签
  

闽ICP备14008679号