Azure OpenAI Assistants 함수 호출(클래식)

다음에만 적용됩니다: Foundry(클래식) 포털. 이 문서는 새 Foundry 포털에서 사용할 수 없습니다. 새 포털에 대해 자세히 알아봅니다.

참고

이 문서의 링크는 현재 보고 있는 Foundry(클래식) 설명서 대신 새 Microsoft Foundry 설명서의 콘텐츠를 열 수 있습니다.

참고

Assistants API는 폐기되었으며 2026년 8월 26일에 폐지될 예정입니다. 일반적으로 제공되는 Microsoft 파운드리 에이전트 서비스를 이용하세요. 마이그레이션 가이드를 따라 워크로드를 업데이트하세요. 자세히 알아보세요.

Assistants API는 함수 호출을 지원하므로 도우미에 대한 함수 구조를 설명한 다음 해당 인수와 함께 호출해야 하는 함수를 반환할 수 있습니다.

함수 호출 지원

지원되는 모델

모델 페이지에는 비서가 지원되는 지역/모델에 대한 최신 정보가 포함되어 있습니다.

병렬 함수를 포함하여 함수 호출의 모든 기능을 사용하려면 2023년 11월 6일 이후에 릴리스된 모델을 사용해야 합니다.

API 버전

.로 2024-02-15-preview시작하는 API 버전

함수 정의 예제

참고

  • 특정 실행에서 file_search, code_interpreter, 또는 function와 같은 특정 도구의 사용을 강제하기 위해 사용할 수 있는 tool_choice 매개 변수에 대한 지원을 추가했습니다.
  • 실행은 만든 후 10분 후에 만료됩니다. 이 만료 전에 도구 출력을 제출해야 합니다.
  • Azure Logic Apps를 사용하여 함수 호출을 수행할 수도 있습니다.
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-07-01-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

assistant = client.beta.assistants.create(
  name="Weather Bot",
  instructions="You are a weather bot. Use the provided functions to answer questions.",
  model="gpt-4", #Replace with model deployment name
  tools=[{
      "type": "function",
    "function": {
      "name": "get_weather",
      "description": "Get the weather in location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {"type": "string", "description": "The city name, for example San Francisco"}
        },
        "required": ["location"]
      }
    }
  }]
)

함수 읽기

함수를 트리거하는 사용자 메시지로 실행을 시작하면 실행 이 보류 중인 상태가 됩니다. 실행은 처리된 후 실행을 검색하여 확인할 수 있는 require_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": "get_weather",
            "arguments": "{\"location\":\"Seattle\"}"
          }
        },
      ]
    }
  },
...

함수 출력 제출

그런 다음 호출하는 함수에서 도구 출력을 제출하여 실행을 완료할 수 있습니다. required_action 객체에서 참조된 tool_call_id 값을 각 함수 호출의 출력과 일치하도록 전달합니다.


# Example function
def get_weather():
    return "It's 80 degrees F and slightly cloudy."

# Define the list to store tool outputs
tool_outputs = []
 
# Loop through each tool in the required action section
for tool in run.required_action.submit_tool_outputs.tool_calls:
  # get data from the weather function
  if tool.function.name == "get_weather":
    weather = get_weather()
    tool_outputs.append({
      "tool_call_id": tool.id,
      "output": weather
    })
 
# Submit all tool outputs at once after collecting them in a list
if tool_outputs:
  try:
    run = client.beta.threads.runs.submit_tool_outputs_and_poll(
      thread_id=thread.id,
      run_id=run.id,
      tool_outputs=tool_outputs
    )
    print("Tool outputs submitted successfully.")
  except Exception as e:
    print("Failed to submit tool outputs:", e)
else:
  print("No tool outputs to submit.")
 
if run.status == 'completed':
  print("run status: ", run.status)
  messages = client.beta.threads.messages.list(thread_id=thread.id)
  print(messages.to_json(indent=2))

else:
  print("run status: ", run.status)
  print (run.last_error.message)

도구 출력을 제출한 후, 실행은 실행을 계속하기 전에 queued 상태로 진입합니다.

다음 항목 참고하기