Chatgpt api로 대화하기, 이미지 전송하기
요즘 만들고 있는 툴킷입니다.
[영상]
1
2
3
4
# requirements.txt
python-dotenv==1.0.1
openai==1.58.1
- image_description_cli.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
# image_description_cli.py
import os
import asyncio
import base64
from openai import AsyncOpenAI
from dotenv import load_dotenv
load_dotenv()
client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def encode_image_to_base64(image_path: str) -> str:
"이미지를 Base64로 인코딩합니다."
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
async def gpt_query_with_image(user_query: str, image_path: str) -> str:
"이미지와 함께 OpenAI API에 요청합니다."
img_b64_str = encode_image_to_base64(image_path)
img_type = "image/jpeg" # 이미지 유형 (필요 시 변경 가능)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": user_query},
{
"type": "image_url",
"image_url": {"url": f"data:{img_type};base64,{img_b64_str}"},
},
],
}
]
response = await client.chat.completions.create(
model="gpt-4o-mini", # 이미지 처리 모델이 필요할 수 있음
messages=messages,
)
return response.choices[0].message.content
async def main():
print("이미지를 포함한 메시지를 OpenAI API로 전송합니다.")
user_query = "다음 이미지를 분석해 주세요."
image_path = "resources/image1.jpg" # 이미지 경로
try:
response = await gpt_query_with_image(user_query, image_path)
print(f"[assistant] {response}")
except FileNotFoundError:
print(f"이미지 파일을 찾을 수 없습니다: {image_path}")
except Exception as e:
print(f"오류 발생: {e}")
if __name__ == "__main__":
asyncio.run(main())
- role_playing_async_cli.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
# role_playing_async_cli.py
import os
from openai import OpenAI
import asyncio
from openai import AsyncOpenAI
from dotenv import load_dotenv
load_dotenv()
client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# 상황극 설정
language = "Korean"
gpt_name = "Steve"
level_string = f"a beginner in {language}"
level_word = "simple"
situation_en = "make new friends"
my_role_en = "me"
gpt_role_en = "new friend"
SYSTEM_PROMPT = (
f"You are helpful expert assistant supporting people learning {language}."
f"Please respond in {language} only."
f"Your name is {gpt_name}. Please assume that the user you are assisting "
f"is {level_string}. And please write only the sentence without "
f"the character role."
)
USER_PROMPT = (
f"Let's have a conversation in {language}. Please answer in {language} only "
f"without providing a translation. And please don't write down the "
f"pronunciation either. Let us assume that the situation in '{situation_en}'. "
f"I am {my_role_en}. The character I want you to act as is {gpt_role_en}. "
f"Please make sure that "
f"I'm {level_string}, so please use {level_word} words as much as possible. "
f"Now, start a conversation with the first sentence!"
)
RECOMMEND_PROMPT = (
f"Can you please provide me an {level_word} example "
f"of how to respond to the last sentence "
f"in this situation, without providing a translation "
f"and any introductory phrases or sentences."
)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
]
async def gpt_query(user_query: str, skip_save: bool = False) -> str:
"유저 메세지에 대한 응답을 반환합니다."
global messages
messages.append(
{
"role": "user",
"content": user_query,
}
)
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
)
assistant_message = response.choices[0].message.content
if not skip_save:
messages.append(
{
"role": "assistant",
"content": assistant_message,
}
)
return assistant_message
async def main():
assistant_message = await gpt_query(USER_PROMPT)
print(f"[assistant] {assistant_message}")
while line := input("[user] ").strip():
if line == "!recommend":
recommended_message = await gpt_query(RECOMMEND_PROMPT, skip_save=True)
print("추천 표현: ", recommended_message)
else:
response = await gpt_query(line)
print(f"[assistant] {response}")
if __name__ == "__main__":
asyncio.run(main())
- role_playing_async_vision_cli.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
134
135
136
137
138
139
140
141
142
143
144
# role_playing_async_vision_cli.py
import os
import asyncio
import base64
from openai import AsyncOpenAI
from dotenv import load_dotenv
load_dotenv()
client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# 상황극 설정
language = "Korean"
gpt_name = "Steve"
level_string = f"a beginner in {language}"
level_word = "simple"
situation_en = "make new friends"
my_role_en = "me"
gpt_role_en = "new friend"
SYSTEM_PROMPT = (
f"You are helpful expert assistant supporting people learning {language}."
f"Please respond in {language} only."
f"Your name is {gpt_name}. Please assume that the user you are assisting "
f"is {level_string}. And please write only the sentence without "
f"the character role."
)
USER_PROMPT = (
f"Let's have a conversation in {language}. Please answer in {language} only "
f"without providing a translation. And please don't write down the "
f"pronunciation either. Let us assume that the situation in '{situation_en}'. "
f"I am {my_role_en}. The character I want you to act as is {gpt_role_en}. "
f"Please make sure that "
f"I'm {level_string}, so please use {level_word} words as much as possible. "
f"Now, start a conversation with the first sentence!"
)
RECOMMEND_PROMPT = (
f"Can you please provide me an {level_word} example "
f"of how to respond to the last sentence "
f"in this situation, without providing a translation "
f"and any introductory phrases or sentences."
)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
]
def encode_image_to_base64(image_path: str) -> str:
"이미지를 Base64로 인코딩합니다."
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
async def gpt_query(
user_query: str, image_path: str = None, skip_save: bool = False
) -> str:
"유저 메시지와 이미지에 대한 응답을 반환합니다."
global messages
if image_path:
img_b64_str = encode_image_to_base64(image_path)
img_type = "image/jpeg" # 이미지 유형
messages.append(
{
"role": "user",
"content": [
{"type": "text", "text": user_query},
{
"type": "image_url",
"image_url": {"url": f"data:{img_type};base64,{img_b64_str}"},
},
],
}
)
else:
messages.append({"role": "user", "content": user_query})
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
)
assistant_message = response.choices[0].message.content
if not skip_save:
messages.append({"role": "assistant", "content": assistant_message})
return assistant_message
async def gpt_query_with_image(user_query: str, image_path: str) -> str:
"이미지와 함께 OpenAI API에 요청합니다."
img_b64_str = encode_image_to_base64(image_path)
img_type = "image/jpeg" # 이미지 유형 (필요 시 변경 가능)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": user_query},
{
"type": "image_url",
"image_url": {"url": f"data:{img_type};base64,{img_b64_str}"},
},
],
}
]
response = await client.chat.completions.create(
model="gpt-4o-mini", # 이미지 처리 모델이 필요할 수 있음
messages=messages,
)
return response.choices[0].message.content
async def main():
assistant_message = await gpt_query(USER_PROMPT)
print(f"[assistant] {assistant_message}")
while line := input("[user] ").strip():
if line.startswith("!image:"):
image_path = line[len("!image:") :].strip()
try:
response = await gpt_query_with_image("너에게 이 이미지를 보여주고 싶어.:", image_path=image_path)
print(f"[assistant] {response}")
except FileNotFoundError:
print(f"[assistant] 이미지 파일을 찾을 수 없습니다: {image_path}")
except Exception as e:
print(f"[assistant] 오류 발생: {e}")
elif line == "!recommend":
recommended_message = await gpt_query(RECOMMEND_PROMPT, skip_save=True)
print("추천 표현: ", recommended_message)
else:
response = await gpt_query(line)
print(f"[assistant] {response}")
if __name__ == "__main__":
asyncio.run(main())
- role_playing_cli.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
# role_playing_cli.py
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# 상황극 설정
language = "Korean"
gpt_name = "Steve"
level_string = f"a beginner in {language}"
level_word = "simple"
situation_en = "make new friends"
my_role_en = "me"
gpt_role_en = "new friend"
SYSTEM_PROMPT = (
f"You are helpful assistant supporting people learning {language}. "
f"Your name is {gpt_name}. Please assume that the user you are assisting "
f"is {level_string}. And please write only the sentence without "
f"the character role."
)
USER_PROMPT = (
f"Let's have a conversation in {language}. Please answer in {language} only "
f"without providing a translation. And please don't write down the "
f"pronunciation either. Let us assume that the situation in '{situation_en}'. "
f"I am {my_role_en}. The character I want you to act as is {gpt_role_en}. "
f"Please make sure that "
f"I'm {level_string}, so please use {level_word} words as much as possible. "
f"Now, start a conversation with the first sentence!"
)
RECOMMEND_PROMPT = (
f"Can you please provide me an {level_word} example "
f"of how to respond to the last sentence "
f"in this situation, without providing a translation "
f"and any introductory phrases or sentences."
)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
]
def gpt_query(user_query: str, skip_save: bool = False) -> str:
"유저 메세지에 대한 응답을 반환합니다."
global messages
messages.append(
{
"role": "user",
"content": user_query,
}
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
)
assistant_message = response.choices[0].message.content
if not skip_save:
messages.append(
{
"role": "assistant",
"content": assistant_message,
}
)
return assistant_message
def main():
assistant_message = gpt_query(USER_PROMPT)
print(f"[assistant] {assistant_message}")
while line := input("[user] ").strip():
if line == "!recommend":
recommended_message = gpt_query(RECOMMEND_PROMPT, skip_save=True)
print("추천 표현: ", recommended_message)
else:
response = gpt_query(line)
print(f"[assistant] {response}")
if __name__ == "__main__":
main()
더 완성되면 또 공유드리겠습니다~
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

