-
OpenAI Assistant Tools인턴 2023. 12. 28. 16:58
1. Code Interpreter
- 다양한 데이터와 포맷의 파일 처리 가능하며, 데이터와 그래프 이미지가 포함된 파일 생성 가능
- 코드 실행이 성공할 때까지 다른 코드를 실행하여 어려운 코드 및 수학 문제 해결 가능
- Assistant가 두 개의 서로 다른 thread에서 동시에 code interpreter를 호출하는 경우 두개의 code interpreter 세션이 생성됨. (기본 1시간 활성화)
- retrieval과 동시에 설정 불가
# Upload a file with an "assistants" purpose file = client.files.create( file=open("speech.py", "rb"), purpose='assistants' ) # Create an assistant using the file ID assistant = client.beta.assistants.create( instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.", # code interpreter을 호출할 시기 결정 model="gpt-4-1106-preview", tools=[{"type": "code_interpreter"}], file_ids=[file.id] ) thread = client.beta.threads.create( messages=[ { "role": "user", "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?", "file_ids": [file.id] # 파일은 특정 스레드에서만 액세스 가능 / endpoint 사용하여 파일 업로드 } ] )
code interpreter에서 생성된 이미지 및 파일 읽기
1. 이미지 { "id": "msg_abc123", "object": "thread.message", "created_at": 1698964262, "thread_id": "thread_abc123", "role": "assistant", "content": [ { "type": "image_file", "image_file": { "file_id": "file-abc123" } } ] # ... } # file id API에 전달하여 파일 다운로드 from openai import OpenAI client = OpenAI() image_data = client.files.content("file-abc123") image_data_bytes = image_data.read() with open("./my-image.png", "wb") as file: file.write(image_data_bytes) 2. 데이터 파일(ex. csv) "content": [ { "type": "text", "text": { "value": "The rows of the CSV file have been shuffled and saved to a new CSV file. You can download the shuffled CSV file from the following link:\n\n[Download Shuffled CSV File](sandbox:/mnt/data/shuffled_file.csv)", "annotations": [ { "type": "file_path", "text": "sandbox:/mnt/data/shuffled_file.csv", "start_index": 167, "end_index": 202, "file_path": { "file_id": "file-abc123" } } ...
Code Interpreter의 입출력 로그
run_steps = client.beta.threads.runs.steps.list( thread_id=thread.id, run_id=run.id ) # OUTPUT 예시 { "object": "list", "data": [ { "id": "step_abc123", "object": "thread.run.step", "type": "tool_calls", "run_id": "run_abc123", "thread_id": "thread_abc123", "status": "completed", "step_details": { "type": "tool_calls", "tool_calls": [ { "type": "code", "code": { "input": "# Calculating 2 + 2\nresult = 2 + 2\nresult", "outputs": [ { "type": "logs", "logs": "4" } ... }
2. Knowledge Retrieval
- 모델 외부의 지식으로 Assistant를 Augment
- 파일이 Assistant에 전달되면 OpenAI는 자동으로 문서를 Chunk & 임베딩 저장 & 벡터 검색을 구현하여 관련 콘텐츠를 검색하여 사용자 쿼리에 응답
- 짧은 문서에 대한 파일 콘텐츠 전달 / 긴 문서에 대해 벡터 검색 수행
- 기존 chatGPT에 사용자가 넣어준 외부 지식을 잘 섞어서 응답을 출력해준다고 보면 될듯...?
Retrieval 활성화
assistant = client.beta.assistants.create( instructions="You are a customer support chatbot. Use your knowledge base to best respond to customer queries.", model="gpt-4-1106-preview", tools=[{"type": "retrieval"}] )
벡터 검색의 원리 파일 업로드
# Upload a file with an "assistants" purpose file = client.files.create( file=open("knowledge.pdf", "rb"), purpose='assistants' ) # Add the file to the assistant assistant = client.beta.assistants.create( instructions="You are a customer support chatbot. Use your knowledge base to best respond to customer queries.", model="gpt-4-1106-preview", tools=[{"type": "retrieval"}], file_ids=[file.id] ) message = client.beta.threads.messages.create( thread_id=thread.id, role="user", content="I can not find in the PDF manual how to turn off this device.", file_ids=[file.id] )
파일 제거
# 어시스턴트에서 파일 분리 file_deletion_status = client.beta.assistants.files.delete( assistant_id=assistant.id, file_id=file.id )
3. Function calling
- 함수 호출 & callback 결과를 제공하여 실행을 계속
assistant = client.beta.assistants.create( instructions="You are a weather bot. Use the provided functions to answer questions.", model="gpt-4-1106-preview", tools=[{ "type": "function", "function": { "name": "getCurrentWeather", "description": "Get the weather in location", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["c", "f"]} }, "required": ["location"] } } }, { "type": "function", "function": { "name": "getNickname", "description": "Get the nickname of a city", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"}, }, "required": ["location"] } } }] )
Reading the functions called by the Assistant
- 함수를 트리거하는 user 메시지를 사용하여 Run을 시작하면 run -> pending 상태로 전환
- 처리가 끝나면 required_action 상태가 됨
- 병렬 함수 호출을 사용하여 여러 함수를 한번에 호출 가능
{ "id": "run_abc123", "object": "thread.run", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "requires_action", "required_action": { "type": "submit_tool_outputs", "submit_tool_outputs": { "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": "getCurrentWeather", "arguments": "{\"location\":\"San Francisco\"}" } }, { "id": "call_abc456", "type": "function", "function": { "name": "getNickname", "arguments": "{\"location\":\"Los Angeles\"}" } } ] } }, ...
function output 제출
- required_action의 tool_call_id 전달
run = client.beta.threads.runs.submit_tool_outputs( thread_id=thread.id, run_id=run.id, tool_outputs=[ { "tool_call_id": call_ids[0], "output": "22C", }, { "tool_call_id": call_ids[1], "output": "LA", }, ] )
'인턴' 카테고리의 다른 글
OpenAI Assistants API v2 stream 오브젝트의 in/outbound (0) 2024.06.03 GraphQL (1) - schema, query, response (1) 2024.01.19 GraphQL 알아보기 (0) 2024.01.18 OpenAI Assistant 작동 방식 (0) 2023.12.28