멀티모달 서빙 API 사용

인증키 입력

import requests
import re
import json
import base64

serving_id = 15
bearer_token = '인증키 입력'
genos_url = 'genos 주소'

endpoint = f"{genos_url}/api/gateway/rep/serving/{serving_id}"
headers = dict(Authorization=f"Bearer {bearer_token}")
  • 인증키와 LLM endpoint 를 입력합니다.

# endpoint의 모델이름을 확인합니다
response = requests.get(endpoint + '/v1/models', headers=headers, timeout=30)
model = response.json()['data'][0]['id']
print(model)
  • 서빙 endpoint에서 사용할 모델이름을 가져옵니다.

llm api key
  • LLM 서빙에 권한이 있는 사용자라면 인증키에서 api를 발급 확인할 수 있습니다.

  • 다른사람이 서빙한것의 주소만 공유받아 사용하는것이라면 서빙권한을 가진 사용자에게 인증키 발급을 요청하셔야합니다.

serving api endpoint
  • 서빙 endpoint는 LLM 서빙 상세, 기본 정보에서 확인할수 있습니다.

  • 서빙 endpoint는 LLM 서빙 권한을 가진 사용자에게 요청해야합니다.

API 호출

# 이미지를 로딩합니다.
img_path = './test.jpeg'
image_base64 = base64.b64encode(open(img_path, 'rb').read()).decode('utf-8')

payload = {
        'model': model,
        'messages': [
            {
                'role': 'user',
                'content': [
                    { 'type': 'text', 'text': '이미지를 자세하게 설명해줘' },
                    { 'type': 'image_url', 'image_url': { 'url': f"data:image/jpeg;base64,{image_base64}" }}
                ]
            }
        ]
    }
response = requests.post(endpoint+'/v1/chat/completions', headers=headers, json=payload, timeout=180)
print(response.json()['choices'][0]['message']['content'])
  • 이미지를 로딩하고 base64 인코딩을 진행합니다.

  • 메세지를 구성할때 base64 문자열을 위와 같이 넣어줍니다.

  • 메세지를 입력하고 API 출력을 확인합니다.

  • genos LLM 서빙 API는 openai API 호출 형식과 동일합니다.

Last updated

Was this helpful?