PandasAI Agent — 다회전 대화와 샌드박스 실행

PandasAI Agent — 다회전 대화와 샌드박스 실행

PandasAI의 pai.chat() 메서드가 단발성 탐색 분석용이라면, Agent는 대화의 상태를 유지하는 다회전 대화를 위해 설계됐어요. 메모리를 유지하며 후속 질문을 처리하고, 선택적으로 격리된 샌드박스에서 코드를 실행해 보안을 강화할 수 있어요.

에이전트 만들기

import os
from pandasai import Agent
import pandas as pd

sales_by_country = pd.DataFrame({
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy",
                "Spain", "Canada", "Australia", "Japan", "China"],
    "sales": [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000],
    "deals_opened": [142, 80, 70, 90, 60, 50, 40, 30, 110, 120],
    "deals_closed": [120, 70, 60, 80, 50, 40, 30, 20, 100, 110]
})

agent = Agent(sales_by_country)
agent.chat('Which are the top 5 countries by sales?')
# Output: China, United States, Japan, Germany, Australia

pai.chat()과 달리 Agent는 대화 상태를 기억해서 연속 질문을 처리해요.

agent.chat('And which one has the most deals?')
# Output: United States has the most deals

후속 질문 (Follow-up)

follow_up 메서드는 대화 메모리를 지우지 않고 컨텍스트를 이어가요.

response = agent.chat('What is the total sales?')
follow_up_response = agent.follow_up('What about last year?')

샌드박스 환경에서 실행

프롬프트 인젝션 같은 악성 코드로부터 보호하기 위해 PandasAI는 격리된 Docker 샌드박스에서 코드를 실행하도록 지원해요.

from pandasai import Agent
from pandasai_docker import DockerSandbox

sandbox = DockerSandbox()
sandbox.start()

df = pai.read_csv("data.csv")
agent = Agent([df], sandbox=sandbox)
response = agent.chat("Calculate the average sales")

sandbox.stop()

벡터 스토어로 학습

엔터프라이즈 라이선스가 있다면 ChromaDB·Qdrant·Pinecone·LanceDB 같은 로컬 벡터 스토어를 사용해 few-shot 학습 방식으로 에이전트를 훈련시킬 수도 있어요. 훈련 예제를 넣어두면 유사 질문에 그 지식을 활용해 답변을 생성해요.

더 알아보기 (Learn more)

출처: PandasAI 공식 문서