๐ฌ ์์ฑํ 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 ChatOpenAIfrom langchain.prompts import PromptTemplateimport warningsimport httpximport osfrom dotenv import load_dotenvwarnings.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 osimport warningsimport httpxfrom dotenv import load_dotenvfrom langchain_openai import ChatOpenAIwarnings.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 osfrom dotenv import load_dotenvload_dotenv() 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 httpxfrom langchain_openai import ChatOpenAIclass GPTClient : def __init__ (self, api_key, base_url, model="gemini-1.5-flash" , temperature=0 ): self .client = httpx.Client(verify=False , timeout=30 ) 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 PromptTemplatedef 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_URLfrom gpt_client import GPTClientfrom prompt_templates import create_short_answer_promptif __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.