Codestral로 코드 해석 및 데이터셋 분석하기
Codestral로 코드 해석 및 데이터셋 분석하기 (Codestral with code interpreting and analyzing dataset)
Mistral의 Codestral 모델과 E2B의 코드 인터프리터 SDK를 사용해 데이터셋을 분석하는 AI 어시스턴트를 만드는 문서예요. 안전한 클라우드 샌드박스 안의 Jupyter 서버에서 생성된 코드를 실행합니다.
출처: 문서
본문
이 AI 어시스턴트는 [E2B]의 오픈소스 [Code Interpreter SDK]로 구동돼요. 이 SDK는 [Firecracker]로 구동되는 안전한 클라우드 샌드박스를 빠르게 만들며, 샌드박스 안에는 LLM이 사용할 수 있는 실행 중인 Jupyter 서버가 있습니다.
Mistral의 새 Codestral 모델에 대해 더 알아보려면 [여기]를 참고하세요.
Step 1: 의존성 설치
[E2B 코드 인터프리터 SDK]와 [Mistral의 Python SDK]를 설치하는 것으로 시작해요.
%pip install -r requirements.txt
Step 2: API 키와 프롬프트 정의
Mistral과 E2B의 API 키, 모델 ID, 프롬프트를 정의해요.
이 예시는 아직 도구 사용(함수 호출)을 완전히 지원하지 않는 Mistral 모델에도 범용적으로 동작하도록 만들어졌기 때문에 도구를 정의하지 않아요. Mistral LLM의 함수 호출에 대해 더 알아보려면 [이 문서 페이지]를 참조하세요.
# TODO: Get your Mistral API key from https://console.mistral.ai
MISTRAL_API_KEY = ""
# TODO: Get your E2B API key from https://e2b.dev/docs
E2B_API_KEY = ""
MODEL_NAME = "codestral-latest" #See the available models at https://docs.mistral.ai/getting-started/models/
SYSTEM_PROMPT = """You're a Python data scientist. You are given tasks to complete and you run Python code to solve them.
Information about the csv dataset:
- It's in the `/home/user/global_economy_indicators.csv` file
- The CSV file is using , as the delimiter
- It has the following columns (examples included):
- country: "Argentina", "Australia"
- Region: "SouthAmerica", "Oceania"
- Surface area (km2): for example, 2780400
- Population in thousands (2017): for example, 44271
- Population density (per km2, 2017): for example, 16.2
- Sex ratio (m per 100 f, 2017): for example, 95.9
- GDP: Gross domestic product (million current US$): for example, 632343
- GDP growth rate (annual %, const. 2005 prices): for example, 2.4
- GDP per capita (current US$): for example, 14564.5
- Economy: Agriculture (% of GVA): for example, 10.0
- Economy: Industry (% of GVA): for example, 28.1
- Economy: Services and other activity (% of GVA): for example, 61.9
- Employment: Agriculture (% of employed): for example, 4.8
- Employment: Industry (% of employed): for example, 20.6
- Employment: Services (% of employed): for example, 74.7
- Unemployment (% of labour force): for example, 8.5
- Employment: Female (% of employed): for example, 43.7
- Employment: Male (% of employed): for example, 56.3
- Labour force participation (female %): for example, 48.5
- Labour force participation (male %): for example, 71.1
- International trade: Imports (million US$): for example, 59253
- International trade: Exports (million US$): for example, 57802
- International trade: Balance (million US$): for example, -1451
- Education: Government expenditure (% of GDP): for example, 5.3
- Health: Total expenditure (% of GDP): for example, 8.1
- Health: Government expenditure (% of total health expenditure): for example, 69.2
- Health: Private expenditure (% of total health expenditure): for example, 30.8
- Health: Out-of-pocket expenditure (% of total health expenditure): for example, 20.2
- Health: External health expenditure (% of total health expenditure): for example, 0.2
- Education: Primary gross enrollment ratio (f/m per 100 pop): for example, 111.5/107.6
- Education: Secondary gross enrollment ratio (f/m per 100 pop): for example, 104.7/98.9
- Education: Tertiary gross enrollment ratio (f/m per 100 pop): for example, 90.5/72.3
- Education: Mean years of schooling (female): for example, 10.4
- Education: Mean years of schooling (male): for example, 9.7
- Urban population (% of total population): for example, 91.7
- Population growth rate (annual %): for example, 0.9
- Fertility rate (births per woman): for example, 2.3
- Infant mortality rate (per 1,000 live births): for example, 8.9
- Life expectancy at birth, female (years): for example, 79.7
- Life expectancy at birth, male (years): for example, 72.9
- Life expectancy at birth, total (years): for example, 76.4
- Military expenditure (% of GDP): for example, 0.9
- Population, female: for example, 22572521
- Population, male: for example, 21472290
- Tax revenue (% of GDP): for example, 11.0
- Taxes on income, profits and capital gains (% of revenue): for example, 12.9
- Urban population (% of total population): for example, 91.7
Generally, you follow these rules:
- ALWAYS FORMAT YOUR RESPONSE IN MARKDOWN
- ALWAYS RESPOND ONLY WITH CODE IN CODE BLOCK LIKE THIS:
```python
{code}
- the Python code runs in jupyter notebook.
- every time you generate Python, the code is executed in a separate cell. it's okay to make multiple calls to
execute_python. - display visualizations using matplotlib or any other visualization library directly in the notebook. don't worry about saving the visualizations to a file.
- you have access to the internet and can make api requests.
- you also have access to the filesystem and can read/write files.
- you can install any pip package (if it exists) if you need to be running
!pip install {package}. The usual packages for data analysis are already preinstalled though. - you can run any Python code you want, everything is running in a secure sandbox environment """
모델이 Markdown으로 메시지를 반환하도록 지시한 뒤, Python 코드 블록을 파싱해 추출해요.
```python
import re
pattern = re.compile(r'```python\n(.*?)\n```', re.DOTALL) # Match everything in between ```python and ```
def match_code_block(llm_response):
match = pattern.search(llm_response)
if match:
code = match.group(1)
print(code)
return code
return ""
Step 3: 코드 해석 메서드 구현
E2B 코드 인터프리터 SDK를 사용하는 주요 함수예요. 아래 코드에서 Codestral의 응답을 도구 호출로 파싱할 때 이 함수를 호출할 거예요.
def code_interpret(e2b_code_interpreter, code):
print("Running code interpreter...")
exec = e2b_code_interpreter.notebook.exec_cell(
code,
on_stderr=lambda stderr: print("[Code Interpreter]", stderr),
on_stdout=lambda stdout: print("[Code Interpreter]", stdout),
# You can also stream code execution results
# on_result=...
)
if exec.error:
print("[Code Interpreter ERROR]", exec.error)
else:
return exec.results
Step 4: Codestral 호출 및 응답 파싱 메서드 구현
이제 chat 메서드를 정의하고 구현할게요. 이 메서드에서 Codestral LLM을 호출하고, 출력에서 Python 코드 블록을 추출하며, 위에서 정의한 code_interpret 메서드를 호출해요.
from mistralai.client import MistralClient
client = MistralClient(api_key=MISTRAL_API_KEY)
def chat(e2b_code_interpreter, user_message):
print(f"\n{'='*50}\nUser message: {user_message}\n{'='*50}")
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message}
]
response = client.chat(
model=MODEL_NAME,
messages=messages,
)
response_message = response.choices[0].message
python_code = match_code_block(response_message.content)
if python_code != "":
code_interpreter_results = code_interpret(e2b_code_interpreter, python_code)
return code_interpreter_results
else:
print(f"Failed to match any Python code in model's response {response_message}")
return[]
Step 5: 코드 인터프리터 샌드박스에 데이터셋 업로드 메서드 구현
파일이 코드 인터프리터가 실행 중인 E2B 샌드박스에 업로드돼요. 파일의 원격 경로는 remote_path 변수에서 얻을 수 있어요.
def upload_dataset(code_interpreter):
print("Uploading dataset to Code Interpreter sandbox...")
with open("./global_economy_indicators.csv", "rb") as f:
remote_path = code_interpreter.upload_file(f)
print("Uploaded at", remote_path)
Step 6: 모두 합치기
마지막 단계에서는 모든 조각을 결합해요. CodeInterpreter(api_key=E2B_API_KEY)로 새 코드 인터프리터 인스턴스를 만들고, 사용자 메시지와 code_interpreter 인스턴스로 chat 메서드를 호출해요.
from e2b_code_interpreter import CodeInterpreter
with CodeInterpreter(api_key=E2B_API_KEY) as code_interpreter:
# Upload the dataset to the code interpreter sandbox
upload_dataset(code_interpreter)
code_results = chat(
code_interpreter,
"Make a chart showing linear regression of the relationship between GDP per capita and life expectancy from the global_economy_indicators. Filter out any missing values or values in wrong format."
)
if code_results:
first_result = code_results[0]
else:
raise Exception("No code interpreter results")
# This will render the image
# You can also access the data directly
# first_result.png
# first_result.jpg
# first_result.pdf
# ...
first_result
이 흐름에서 Codestral이 GDP 대비 기대 수명의 선형 회귀 차트를 그리는 Python 코드를 생성하고, E2B 샌드박스의 Jupyter에서 실행되며, 결과가 이미지로 반환돼요.