-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.py
134 lines (90 loc) · 2.9 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import requests
import json
import os
import openai
def chat2(word,chat):
# リクエストに必要なパラメーター
headers = {'content-type':'text/json'}
payload = {'utterance':word,
"username":"マスター",
"agentState":{"agentName":"Salieri","tone":"normal", "age":"0歳"},}
# APIKEYの部分は自分のAPI鍵を代入してください
url = 'https://www.chaplus.jp/v1/chat?apikey=629d99df19335'
# APIを叩く
res = requests.post(url=url, headers=headers, data=json.dumps(payload))
# 最適と思われるレスポンスを抽出
print(res.json()['bestResponse']['utterance'])
return res.json()['bestResponse']['utterance'],word
#print(chat2('こんにちは'))
def chat_gpt(text,chat):
prompt = chat
f = open('api.txt', 'r',encoding='UTF-8')
api = f.read()
f = open('txt/chat.txt', 'r',encoding='UTF-8')
prompt= f.read()
openai.api_key = api
start_sequence = "\nサリエリ:"
restart_sequence = "\nYou:"
input=text
input_sq=restart_sequence+input+start_sequence
#print(prompt,chat,input_sq)
prompt=prompt+chat+input_sq
response = openai.Completion.create(
engine="text-davinci-002",
#engine="text-curie-001",
prompt=prompt,
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=["You:", "サリエリ:"]
)
res=response['choices'][0]['text']
print(prompt+res)
return res,prompt
def chat_emoji(text,chat):
prompt = chat
f = open('api.txt', 'r',encoding='UTF-8')
api = f.read()
openai.api_key = api
input=text
prompt="映画のタイトルを絵文字に変換します。\n\nバック・トゥ・ザ・フューチャー:👨👴🚗🕒\nバットマン:🤵🦇\nトランスフォーマー:🚗🤖\nスターウォーズ:⭐️🌌\n"
prompt=prompt+input+":"
#print(prompt)
response = openai.Completion.create(
model="text-davinci-002",
prompt=prompt,
temperature=0.8,
max_tokens=60,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
return response['choices'][0]['text'],prompt
def chat_eng(text,chat):
f = open('api.txt', 'r',encoding='UTF-8')
api = f.read()
f = open('txt/chat_jpn_eng.txt', 'r',encoding='UTF-8')
prompt= f.read()
openai.api_key = api
start_sequence = "\nSalieri:"
restart_sequence = "\nYou:"
input=text
input_sq=restart_sequence+input+start_sequence
#print(prompt,chat,input_sq)
prompt=prompt+chat+input_sq
response = openai.Completion.create(
model="text-davinci-002",
prompt=prompt,
temperature=0,
max_tokens=100,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
res=response['choices'][0]['text']
print(prompt+res)
return res,prompt