ChatGPT最近很红火吧,要颠覆很多行业,大家都想试试,但是其开发公司OPENAI早有预见的封锁了中国的IP,大家都上不去体验不了,闹心吧,干着急,于是有些人用微信开了些转接小程序,但这一行为也游走在了一些边缘地带。
逛CSDN的显然不是网络小白,大小也得是程序员,虽然程序员在网络中都颇有办法,但魔法上网是违法的也希望大家明白。今天给大家一个合法的思路使用CHATGPT。因为我早早就有账号了,这个博客不教如何注册,就说怎么正规的用,首先买一个短期的阿里云的云服务器,服务器地点要在美国境内。。。对我们要开个一周左右的美国网站挣美国人钱这么个布局,然后找所有服务器中第二便宜的,因为我们要用WINDOWS系统,用最便宜的内存撑不住,选好付款大概30块人民币一周时长,然后远程登路服务器,现在你可以自己安装个浏览器去体验了,就在云服务器上体验就好。
然后再说说API调用,先要在官网上注册并登录后有专用地址获取API然后把这个API保存好,粘在我后面提供的代码里,先说清楚如果是在国内那是肯定连不上的,这个PYTHON程序也得部署在美国的阿里云服务器上跑才能正常工作
import os
import openai
openai. api_key = "sk-j2nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx8E" while 1 : prompt = input ( """你想问什么?:""" ) response = openai. Completion. create( model= "text-davinci-003" , prompt= prompt, max_tokens= 1000 , temperature= 0.7 ) print ( response[ 'choices' ] [ 0 ] [ 'text' ] )
最后,其实这个API模型不是CHATGPT最新的模型,要比较傻一些,但毕竟是官方出的API智能接口再傻也比那些别的智能好很多,但是我没有这方面需求,没有继续用网页请求的方式去调用,至于4.0目前API还不能调用。
现在OPENAI已经把很多IP都封锁了,像ALIYUN这种服务商的美国IP已经无法进入对话界面了。特此提醒。
下面这个程序能记住自己说过的一句话。。。
import openaiopenai. api_key = "sk-p9kHKa----------------------------------ITeFpk" def ask ( question, context= None ) : if context is None : prompt = questionelse : prompt = f" { context} \n { question} " response = openai. Completion. create( engine= "text-davinci-003" , prompt= prompt, max_tokens= 1024 , n= 1 , stop= None , temperature= 0.5 , frequency_penalty= 0.5 , presence_penalty= 0.5 , ) if context is None : return response. choices[ 0 ] . text. strip( ) else : return response. choices[ 0 ] . text. replace( context, "" ) . strip( )
context = None while True : question = input ( "您的问题: " ) answer = ask( question, context= context) context = answerprint ( "CHATGPT的回答: " , answer)
下面这个代码完全是CHATGPT写的,让它写个TK界面然后自己联网并在界面里完成对话,我只提供模型选择和APIKEY两个参数,其他都是它自己做的
import tkinter as tk
import openaiopenai. api_key = "sk-p9kHK-------------------------Za7SITeFpk"
model_engine = "text-davinci-003" class ChatGPT ( tk. Frame) : def __init__ ( self, master= None ) : super ( ) . __init__( master) self. master = masterself. master. title( "ChatGPT" ) self. pack( ) self. create_widgets( ) self. previous_response = None self. prompt = "请问您有什么需要帮忙的?" def create_widgets ( self) : self. conversation = tk. Text( self) self. conversation. config( state= tk. DISABLED, height= 20 , width= 70 ) self. conversation. pack( side= "top" ) self. entry = tk. Entry( self) self. entry. pack( side= "left" , expand= True , fill= "both" ) self. entry. bind( "<Return>" , self. send_message) self. button = tk. Button( self) self. button[ "text" ] = "发送" self. button[ "command" ] = self. send_messageself. button. pack( side= "right" ) def send_message ( self, event= None ) : message = self. entry. get( ) if not message: return self. entry. delete( 0 , tk. END) self. prompt += "\n" + message + "\n" response, _, self. previous_response = generate_response( self. prompt, model_engine, self. previous_response) self. prompt += responseself. conversation. config( state= tk. NORMAL) self. conversation. insert( tk. END, "你:" + message + "\n" , "bold" ) self. conversation. insert( tk. END, "ChatGPT:" + response + "\n\n" ) self. conversation. tag_configure( "bold" , font= ( "TkDefaultFont" , 10 , "bold" ) ) self. conversation. config( state= tk. DISABLED) def generate_response ( prompt, model_engine, previous_response= None ) : response = openai. Completion. create( engine= model_engine, prompt= prompt, temperature= 0.7 , max_tokens= 1024 , n= 1 , stop= None , frequency_penalty= 0.5 , presence_penalty= 0.5 , ) return response. choices[ 0 ] . text, response. choices[ 0 ] . finish_reason, response. choices[ 0 ] . indexroot = tk. Tk( )
app = ChatGPT( master= root)
app. mainloop( )
我觉得只要说的明白,会DEBUG帮它排除错误的话,是可以快速生产应用代码的,就是多少都要改一点。