인턴
OpenAI Assistant 작동 방식
imsmile2000
2023. 12. 28. 14:48
- Assistant는 personality와 function을 설정할 수 있다
- Assistant는 여러 tools에 동시에 access할 수 있다.
- Code Interpreter
- Knowledge retrieval
- Function calling
- Assistant는 persistant Thread에 접근할 수 있다.
- 다양한 형식의 파일에 액세스할 수 있다.(이미지, csv 파일 등)
Objects
Assistant | OpenAI의 모델과 호출 도구를 사용하는 전용 AI |
Thread | 어시스턴트와 사용자 간의 대화 세션. 스레드는 메시지를 저장하고 모델의 컨텍스트에 내용을 맞추기 위해 자동으로 잘라내기를 처리합니다. |
Message | 어시스턴트 또는 사용자가 만든 메시지입니다. 메시지에는 텍스트, 이미지 및 기타 파일이 포함될 수 있습니다. 스레드에 목록으로 저장된 메시지. 모델의 컨텍스트 길이를 초과하면 가장 오래된 메시지를 삭제 |
Run | 스레드에서 어시스턴트를 호출합니다. 어시스턴트는 구성과 스레드의 메시지를 사용하여 모델과 도구를 호출하여 작업을 수행합니다. 실행의 일부로 어시스턴트는 스레드에 메시지를 추가합니다. |
Run Step | 실행의 일부로 보조자가 취한 세부 단계 목록입니다. 보조자는 실행 중에 도구를 호출하거나 메시지를 만들 수 있습니다. 실행 단계를 조사하면 보조자가 최종 결과에 어떻게 도달하는지 자세히 살펴볼 수 있습니다. |
Assistant 생성
# 1. 파일 생성
file = client.files.create(
file=open("speech.py", "rb"),
purpose='assistants'
)
# 2. 업로드된 파일로 어시스턴트 생성
assistant = client.beta.assistants.create(
name="Data visualizer",
description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",
model="gpt-4-1106-preview",
tools=[{"type": "code_interpreter"}],
file_ids=[file.id]
# 최대 20개 파일 첨부 가능
# 3. thread 생성
thread = client.beta.threads.create(
messages=[
{
"role": "user", # 사용자 또는 어시스턴트
"content": "Create 3 data visualizations based on the trends in this file.",
"file_ids": [file.id]
}
]
)
Annotation
- file_citation: retrieval tool이 응답을 생성하기 위해 사용한 특정 파일의 인용에 대한 참조 정의
- file_path: code_interpreter tool에 의해 생성된 파일에 대한 참조 포함
문자열을 annotation에 있는 정보로 바꾸는 코드
# message 반환
message = client.beta.threads.messages.retrieve(
thread_id="...",
message_id="..."
)
# message_content 반환
message_content = message.content[0].text
annotations = message_content.annotations
citations = []
# annotation 반복 & 각주 추가
for index, annotation in enumerate(annotations):
# Replace the text with a footnote
message_content.value = message_content.value.replace(annotation.text, f' [{index}]')
# Gather citations based on annotation attributes
if (file_citation := getattr(annotation, 'file_citation', None)):
cited_file = client.files.retrieve(file_citation.file_id)
citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
elif (file_path := getattr(annotation, 'file_path', None)):
cited_file = client.files.retrieve(file_path.file_id)
citations.append(f'[{index}] Click <here> to download {cited_file.filename}')
# Note: File download functionality not implemented above for brevity
# Add footnotes to the end of the message before displaying to user
message_content.value += '\n' + '\n'.join(citations)
Runs & Run Steps
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
model="gpt-4-1106-preview",
instructions="New instructions that override the Assistant instructions",
tools=[{"type": "code_interpreter"}, {"type": "retrieval"}] # 다 정의 가능
)
Run lifecycle
queued | runs가 처음 생성되거나 required_action이 완료되면 in_progress 상태로 이동 |
in_progress | 모델과 tools를 사용하여 단계 수행. run steps를 통해 run 진행 상황 확인 |
completed | Run 완료된 상태. Assistant가 수행한 모든 단계 확인 가능 |
requires_actions | Function calling tool을 사용하면 run -> required_action 상태로 이동 Run이 실행되기 전에 required_action 함수를 실행하고 출력을 제출해야함. expires_at이 10분 동안 출력이 안되면 expired 상태로 변경됨 |
expired | Function calling의 output이 expires_at 이전에 안나오면 발생. 또는 실행이 너무 오래걸리고 expires_at(10분) 초과하면 run이 만료됨 |
cancelling | in_progress run을 취소할 수 있음 Cancelling 시도가 성공하면 cancelled 상태로 이동 |
cancelled | Run이 성공적으로 취소됨 |
failed | 실패의 타임스탬프는 failed_at 아래에 기록됨 |
Run Steps
- message_creation: Assistant가 thread에서 메시지를 생성할 때 사용
- tool_calls: 어시스턴트가 tools를 호출할 때 생성.