Gemini API - 2. API ํ‚ค ๋ฐœ๊ธ‰

๐Ÿ’ฌ ์ƒ์„ฑํ˜• ai ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ

์ƒ์„ฑํ˜• ai api ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™”๋ฉด ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ๊ธ€์„ ์ •๋ฆฌํ•œ๋‹ค
์ฒ˜์Œ๋ถ€ํ„ฐ ๋ˆ์„ ์ฃผ๊ณ  ํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋Š”๊ฒƒ ๋ณด๋‹ค๋Š” ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋˜๋Š” ์ œ๋ฏธ๋‚˜์ด api ํ‚ค๋ฅผ ํ†ตํ•ด์„œ
๊ฐ์„ ์ตํžˆ๊ณ  ๋ˆ์ฃผ๊ณ  ์“ฐ๋Š”๊ฑด ์–ด๋А์ •๋„ ์ต์ˆ™ํ•ด ์ง€๋ฉด ์ง„ํ–‰ํ•˜์ž

ํ‚ค๋ฐœ๊ธ‰

  • ํ”„๋กœ์ ํŠธ๊ฐ€ ์—†์œผ๋ฉด ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  api ํ‚ค๋ฅผ ํด๋ฆญํ•˜์—ฌ ํ‚ค๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค. ์š”๊ธˆ์ œ๋Š” Free๋กœ

์˜์กด์„ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

  • ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์•„๋ž˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•œ๋‹ค
  • dotenv๋Š” apiํ‚ค๋ฅผ ์ˆจ๊ธฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ณ  langchain ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ œ๋ฏธ๋‚˜์ด api ์‚ฌ์šฉ์„ ์œ„ํ•ด ์„ค์น˜ํ•จ
    1
    2
    3
    pip install  dotenv
    pip install langchain
    pip install langchain_openai

api ํ‚ค ์ˆจ๊ธฐ๊ธฐ

โš ๏ธ api ํ‚ค ์ˆจ๊น€

github์— ์†Œ์Šค๋ฅผ ์˜ฌ๋ฆดํ…๋ฐ ํ‚ค๊ฐ€ ๋…ธ์ถœ๋˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ .envํŒŒ์ผ์€ ๋กœ์ปฌ์—๋งŒ ์ €์žฅํ•œ๋‹ค.

  • ํ”„๋กœ์ ํŠธ ์ตœ ์ƒ๋‹จ์— .env ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณต์‚ฌํ•œ api ํ‚ค๋ฅผ ๋ถ™์—ฌ ๋„ฃ๊ธฐ ํ•œ๋‹ค.

  • github์— ํ‚ค๊ฐ€ ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š๋„๋ก .gitignore์— .env๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
1
2
3
4
5
6
7
8
9
/.idea/
/node_modules/
/public/
/.deploy_git/
/.org_img/
/dist/
**/build/
*.exe
.env

ํ…Œ์ŠคํŠธ 1

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
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
import warnings
import httpx
import os
from dotenv import load_dotenv
warnings.simplefilter("ignore")
client = httpx.Client(verify=False, timeout=30)

load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")

template = "{question}(500๊ธ€์ž ์ด๋‚ด๋กœ ๋‹ต๋ณ€)"
prompt = PromptTemplate.from_template(template=template)

gpt = ChatOpenAI(
model="gemini-1.5-flash",
temperature=0,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
api_key=api_key,
http_client=client,
)

final_prompt = prompt.format(question="gemini api ๋Š” ๋ฌด์—‡์ธ๊ฐ€?")
response = gpt.invoke(final_prompt)

print(response.content)

ํ…Œ์ŠคํŠธ1 ๊ฒฐ๊ณผ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Gemini API๋Š” ๊ตฌ๊ธ€์—์„œ ๊ฐœ๋ฐœํ•œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ(LLM)์ธ Gemini๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณตํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.  ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ์œผ๋ฉฐ, ํ…์ŠคํŠธ ์ƒ์„ฑ, ๋ฒˆ์—ญ, ์งˆ๋ฌธ ์‘๋‹ต, ์ฝ”๋“œ ์ƒ์„ฑ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.  ๊ฐœ๋ฐœ์ž๋Š” ์ด API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋‚˜ ์„œ๋น„์Šค์— Gemini์˜ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์„ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Gemini API๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ชจ๋ธ ํฌ๊ธฐ์™€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•ฉ๋‹ˆ๋‹ค. ์ž‘์€ ๋ชจ๋ธ์€ ์†๋„์™€ ํšจ์œจ์„ฑ์„ ์ค‘์‹œํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ํ•ฉํ•˜๋ฉฐ, ํฐ ๋ชจ๋ธ์€ ๋”์šฑ ์ •๊ตํ•˜๊ณ  ๋ณต์žกํ•œ ์ž‘์—…์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, API๋Š” ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ง€์›ํ•˜์—ฌ ๊ฐœ๋ฐœ์ž์˜ ํŽธ์˜์„ฑ์„ ๋†’์ž…๋‹ˆ๋‹ค.

Gemini API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

* **ํ…์ŠคํŠธ ์ƒ์„ฑ:** ์ด์•ผ๊ธฐ, ์ฝ”๋“œ, ์ด๋ฉ”์ผ, ๊ธฐํƒ€ ํ…์ŠคํŠธ ํ˜•์‹์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
* **๋ฒˆ์—ญ:** ํ•œ ์–ธ์–ด์—์„œ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค.
* **์งˆ๋ฌธ ์‘๋‹ต:** ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
* **์š”์•ฝ:** ๊ธด ํ…์ŠคํŠธ๋ฅผ ์š”์•ฝํ•ฉ๋‹ˆ๋‹ค.
* **๋ถ„๋ฅ˜:** ํ…์ŠคํŠธ๋ฅผ ๋‹ค์–‘ํ•œ ๋ฒ”์ฃผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค.
* **์ฝ”๋“œ ์ƒ์„ฑ:** ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

Gemini API๋Š” ๊ตฌ๊ธ€ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ์„ ํ†ตํ•ด ์ œ๊ณต๋˜๋ฉฐ, ์‚ฌ์šฉ๋Ÿ‰์— ๋”ฐ๋ผ ๋น„์šฉ์ด ์ฒญ๊ตฌ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋Š” API ๋ฌธ์„œ์™€ ๋‹ค์–‘ํ•œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด Gemini API๋ฅผ ์‰ฝ๊ฒŒ ํ•™์Šตํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋ชจ๋“  LLM๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, Gemini API์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ฑฐ๋‚˜ ์ ์ ˆํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ฒฐ๊ณผ๋ฅผ ์‹ ์ค‘ํ•˜๊ฒŒ ๊ฒ€ํ† ํ•˜๊ณ  ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


ํ…Œ์ŠคํŠธ2

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
import os
import warnings

import httpx
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

warnings.simplefilter("ignore")
client = httpx.Client(verify=False, timeout=30)

load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")

gpt = ChatOpenAI(
model="gemini-1.5-flash",
temperature=0,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
api_key=api_key,
http_client=client,
)
msg = ''' ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์šฉ์ดํ•˜๊ฒŒ ์•„๋ž˜์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋งํ•ด์ค˜ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ถ€๋ถ„์€ ๋ชจ๋“ˆ๋กœ ๋”ฐ๋กœ ๋นผ์ฃผ๊ณ 
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
import warnings
import httpx
import os
from dotenv import load_dotenv
warnings.simplefilter("ignore")
client = httpx.Client(verify=False, timeout=30)

load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")

template = "{question}(500๊ธ€์ž ์ด๋‚ด๋กœ ๋‹ต๋ณ€)"
prompt = PromptTemplate.from_template(template=template)

gpt = ChatOpenAI(
model="gemini-1.5-flash",
temperature=0,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
api_key=api_key,
http_client=client,
)

final_prompt = prompt.format(question="gemini api ๋Š” ๋ฌด์—‡์ธ๊ฐ€?")
response = gpt.invoke(final_prompt)

print(response.content)
'''
response = gpt.invoke(msg)

print(response.content)

ํ…Œ์ŠคํŠธ2 ๊ฒฐ๊ณผ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
This refactored code separates concerns into modules for better maintainability and readability.  It also handles potential errors more gracefully.

**1. `config.py` (Configuration):**

```python
import os
from dotenv import load_dotenv

load_dotenv() # Load environment variables from .env file

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if GOOGLE_API_KEY is None:
raise ValueError("GOOGLE_API_KEY environment variable not set.")

API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"

2. gpt_client.py (GPT Client):

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
import httpx
from langchain_openai import ChatOpenAI

class GPTClient:
def __init__(self, api_key, base_url, model="gemini-1.5-flash", temperature=0):
self.client = httpx.Client(verify=False, timeout=30) #Consider using verify=True for production
self.gpt = ChatOpenAI(
model=model,
temperature=temperature,
base_url=base_url,
api_key=api_key,
http_client=self.client,
)

def invoke(self, prompt):
try:
response = self.gpt.invoke(prompt)
return response.content
except httpx.HTTPError as e:
print(f"HTTP Error: {e}")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None

3. prompt_templates.py (Prompt Templates):

1
2
3
4
5
6
7
from langchain.prompts import PromptTemplate

def create_short_answer_prompt(question):
template = "{question}(500๊ธ€์ž ์ด๋‚ด๋กœ ๋‹ต๋ณ€)"
prompt = PromptTemplate.from_template(template=template)
return prompt.format(question=question)

4. main.py (Main Application):

1
2
3
4
5
6
7
8
9
10
11
from config import GOOGLE_API_KEY, API_BASE_URL
from gpt_client import GPTClient
from prompt_templates import create_short_answer_prompt

if __name__ == "__main__":
gpt_client = GPTClient(api_key=GOOGLE_API_KEY, base_url=API_BASE_URL)
prompt = create_short_answer_prompt("gemini api ๋Š” ๋ฌด์—‡์ธ๊ฐ€?")
response = gpt_client.invoke(prompt)
if response:
print(response)

Improvements:

  • Modularity: Code is split into logical units, making it easier to understand, test, and maintain.
  • Configuration: API key and base URL are managed in a separate configuration file, improving security and making it easier to change settings.
  • Error Handling: The GPTClient now includes basic error handling to catch HTTP errors and other exceptions, preventing the program from crashing unexpectedly. More robust error handling might be needed in a production environment.
  • Reusability: The GPTClient and prompt_templates can be reused in other parts of your application.
  • Readability: The code is more concise and easier to read.
  • verify=False Warning: The verify=False argument in httpx.Client disables SSL certificate verification. This is generally insecure and should only be used for development or testing. For production, set verify=True.

Remember to create a .env file in the same directory as your main.py file and add your GOOGLE_API_KEY there:

1
GOOGLE_API_KEY=your_actual_api_key

This structure promotes better organization and makes future modifications and extensions much simpler. Consider adding more sophisticated logging and error handling for a production-ready application.


๊ณต์œ ํ•˜๊ธฐ