使用前需要至https://platform.openai.com/account/api-keys
申請api-keyTaiwan is a country. 臺灣是我的國家
由於使用API不會記憶你先前說的話, 所以我將要教它的文字寫在text file讀出, 再將希望AI分析的內容從db讀出來接在後面,
目前免費版本有token(按此計算)限制, 它還能上傳圖片和檔案訓練, 當然也是有免費token限制
所以避免它回答時廢話太多, max_tokens
可以設小一點 Taiwan is
若是希望能接續交談, 則要將之前對方對話內容再加到你的新發言之前一起送出a country.
import oracledb
import openai
import sys
openai.api_key = "申請的key"
model_engine = "text-davinci-003"
file = open('D:\AI\訓練.txt',mode='r', encoding='utf-8')
prompt = file.read()
file.close()
user = '帳號'
password = '密碼'
service_name = 'orclpdb'
conn_string = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=你的IP)(PORT=你的port))(CONNECT_DATA=(SERVER=dedicated)(SID=你的SID)))"
with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
conn.autocommit = True
with conn.cursor() as cursor:
sql="select ID,Rule_Text from tableA where Rule_Text is not null and ai_result is null"
for r in cursor.execute(sql):
print(r[0])
for i in range(6):
try:
response = openai.Completion.create(
engine=model_engine,
prompt=prompt + r[1],
max_tokens= 1024,
n=1,
stop=None,
temperature=0.9
)
data = response["choices"][0]["text"].strip()
token=response["usage"]["total_tokens"]#留下token計算費用
conn.cursor().execute("update tableA set ai_result =:dd,token=:tk where id = :id", [data,token,r[0]])
break
except Exception as msg:
if i < 5:#失敗5次就退出
print(msg)
else:
sys.exit()
Taiwan is a country. 臺灣是我的國家