代理分为http代理和socks代理!!!
第一种是只支持http代理、https代理的,那么就要写如下的代码在文件最前面:
- import os
-
- os.environ["http_proxy"] = "http://127.0.0.1:7890"
- os.environ["https_proxy"] = "http://127.0.0.1:7890"
第二种是socks5代理,这种将如下的代码放在文件的最前面:
这里需要安装一个模块:
pip install pysocks
- import socket, socks
-
- socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1080)
- socket.socket = socks.socksocket
示例代码:
- import os
-
- os.environ["http_proxy"] = "http://127.0.0.1:7890"
- os.environ["https_proxy"] = "http://127.0.0.1:7890"
-
- # import socket, socks
- #
- # socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1080)
- # socket.socket = socks.socksocket
-
- import openai
-
- openai.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
-
-
- def get_completion(prompt, model="gpt-3.5-turbo"):
- messages = [{"role": "user", "content": prompt}]
-
- response = openai.ChatCompletion.create(
- model=model,
- messages=messages,
- temperature=0
- )
- return response.choices[0].message["content"]
-
-
- def __test_get_completion():
- print(get_completion("1+1是多少???"))
-
-
- if __name__ == '__main__':
- __test_get_completion()