-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwo_gpt_chatting_testing.py
More file actions
111 lines (86 loc) · 3.39 KB
/
two_gpt_chatting_testing.py
File metadata and controls
111 lines (86 loc) · 3.39 KB
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
from openai import OpenAI
from langchain.prompts import PromptTemplate
# from langchain.chat_models import ChatOpenAI # NO longer supported as of langchain==0.2.0
from langchain_community.chat_models import ChatOpenAI
from typing import List, Dict, Tuple
import json
import os
import sys
import re
import numpy as np
import requests
import time
############## this block is just for import moudles ######
current_path = os.path.dirname(os.path.realpath(__file__))
parent_path = os.path.dirname(current_path)
grand_path = os.path.dirname(parent_path)
sys.path.append(parent_path)
###########################################################
from src.env import get_api_key
OPENAI_API_KEY = get_api_key()
client = OpenAI(api_key=OPENAI_API_KEY)
SYSTEM = "너는 다른 사람들의 이야기 듣는 것을 좋아하는 사람이야. 질문이 들어오면, 이에 대한 기억을 생성해야 해. \
한국어로 말해야 해. 나는 매사에 긍정적인 편이야. 나는 우리 가족을 자랑스럽게 생각해. 3줄 이상 말하지는 마."
def return_user_response(info_str:str=None,):
"""
history example:
Answer 1: ~ \n
Answer 2: ~ \n
system 제약 조건에서 user의 질문에 assistant 기반으로 답변
"""
response = client.chat.completions.create(
#model="gpt-4",
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"{SYSTEM}"},
{"role": "user", "content": f"{info_str}"},
]
)
output = response.choices[0].message.content
return output
def two_gpt_chatting(text:str,
url:str='https://ckkzfwkxjrrfknrj.tunnel-pt.elice.io/proxy/5001/chat',
is_first_chatting:bool=False,
loop:int=0):
#입력으로 들어오는 text는 gpt 질문
#gpt 질문 바탕 user가 text 생성
text = return_user_response(text)
print(f"Answer {loop}: {text}")
start_time = time.time()
data = {'user': 'jonghyo',
'mp3': None,
'text': text,
'first_chatting': is_first_chatting,
"is_text": True}
#유저 답변 생성으로 gpt 질문 생성
headers = {"Content-Type": "application/json"}
response = requests.post(url, data=json.dumps(data), headers=headers)
end_time = time.time()
print(f"\nRespone end in {end_time-start_time}\n")
json_data = json.loads(response.text)
text_info = json_data["text"]
return text_info
if __name__=="__main__":
num_iter = 10
text = "안녕! 이번주에 특별한 활동이 있었니?"
for i in range(num_iter):
print(f"Question {i}: {text}\n")
if i==0:
text= two_gpt_chatting(text=text,
is_first_chatting=True,
loop=i)
else:
text = two_gpt_chatting(text=text,
is_first_chatting=False,
loop=i)
print("sleeping start...")
#time.sleep(300)
data = {'user': 'jonghyo'}
headers = {"Content-Type": "application/json"}
response = requests.post("https://ckkzfwkxjrrfknrj.tunnel-pt.elice.io/proxy/5001/make_story",
data=json.dumps(data),
headers=headers)
print("======================================")
for res in response:
print(res)
print("------------------")