전체 글174 XVII. AI Agent Engine 기본 완성: Python · Agent Loop · Tool Dispatcher · Function Call · Context Block · History Trim # 1. import import os import json import time import logging import inspect from pathlib import Path from datetime import datetime from dataclasses import dataclass, field from typing import Any from dotenv import load_dotenv from openai import OpenAI # ============================================================ # 2. Environment settings # ===========.. 2026. 8. 30. XVI. 작은 AI Agent에 5가지 기능을 추가한 프로그램(끝) # 1. Tools from pathlib import Path def calculator(expr:str)->str: """(20*4)+15와 같은 간단한 산술 표현식을 계산합니다.""" allowed = set("0123456789+-*/().") expr = expr.replace(" ", "") if not expr or not set(expr) allowed: return "오류: 숫자와 +-*/().만 허용됩니다." try: return str(eval(expr)) except (SyntaxError, ZeroDivisionError) as e: .. 2026. 8. 26. XV. 작은 AI Agent 프로그램(실제 LLM -OpenAI 사용) # 1. Tools from pathlib import Path def calculator(expr:str)->str: """(20*4)+15와 같은 간단한 산술 표현식을 계산합니다.""" allowed = set("0123456789+-*/().") expr = expr.replace(" ", "") if not expr or not set(expr) allowed: return "오류: 숫자와 +-*/().만 허용됩니다." try: return str(eval(expr)) except (SyntaxError, ZeroDivisionError) as e: .. 2026. 8. 26. XIV. 작은 AI Agent 프로그램(가짜 LLM Model-script 사용) # 1. 설계 주석 # 설계 # 목표: 사용자로부터 입력받은 문장 # 도구: 계산기, 파일 읽기, 조회 # 반복: 최대 6단계 # 메모리: 메시지 목록 (완료 시 디스크에 저장) # 종료: 모델이 '완료' 도구를 반환하거나 단계 제한에 도달함 # (아래는 영문으로 위와 동일한 내용) # The design # goal a sentence from the user # tools calculator, read_file, lookup # loop at most 6 steps # memory a list of messages, saved to dis.. 2026. 8. 26. XIII. Python 함수 자체를 분석하여, AI가 사용할 수 있는 Tool 설명서(schema)를 자동으로 만드는 방법 # 1. Type Hint 그리고 __annotations__ def search(query:str, limit:int=3, safe:bool=True)->str: mode = "safe" if safe else "open" return f"'{query}'에 대해 {limit}개의 결과물({mode})" print(search("환불 정책")) print(search("개점 시간", limit=1, safe=False)) print(search.__annotations__) #-------- 2. History 분석 def summarise(messages:list[dict], limit:int|None=None)->dict[str,i.. 2026. 8. 25. XII. Python 프로그램이 인터넷 너머의 API 서버와 대화하는 방법 → 오류, 재시도 ...→ 결국 실제 LLM 호출 함수 call_model()로 발전시키는 과정 # 1. HTTP 상태 코드 # 200 OK, it worked # 400 bad request, your JSON or parameters are wrong # 401 unauthorised, the API key is missing or invalid # 403 forbidden, the key is valid but not allowed to do this # 404 not found, check the URL # 429 too many requests, slow down and retry later # 500 server error on their side # 503 service unavailable, retry later #-------- .. 2026. 8. 25. 이전 1 2 3 4 ··· 29 다음