QQ / API_example.md
Hyungseoky's picture
Update API_example.md
d5b0a13 verified
|
Raw
History Blame Contribute Delete
7.08 kB

3๊ฐœ ์ปฌ๋ ‰์…˜ ํ˜ธ์ถœํ•˜๋Š” ๋ฒ• (ํ…์ŠคํŠธ ์ปฌ๋ ‰์…˜ : page, ์ด๋ฏธ์ง€ - colqwen, ์ด๋ฏธ์ง€ - tomoro)

โœ… 1. ํ…์ŠคํŠธ ์ฟผ๋ฆฌ - page ์ปฌ๋ ‰์…˜ ๊ฒ€์ƒ‰

๐ŸŸข 1-1. ํ•„ํ„ฐ ์—†์ด ๊ฒ€์ƒ‰

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "๋ฌผ๋ฅ˜์—…์ฒด ์„ ์ • ๊ธฐ์ค€",
    "top_k": 3
  }'

๐ŸŸข 1-2. ๋‹จ์ผ ํ•„ํ„ฐ ์ ์šฉ

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "DSG",
    "top_k": 3,
    "filters": {
      "apply_region": "2๊ณต์žฅ"
    }
  }'

๐ŸŸข 1-3. ๋‹ค์ค‘ ํ•„ํ„ฐ ์ ์šฉ (AND ์กฐ๊ฑด)

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "DSG ์ƒ์ •๋ฐ˜ ๊ต์ฒด ์ ˆ์ฐจ",
    "top_k": 3,
    "filters": {
      "apply_region": "3๊ณต์žฅ",
      "apply_product": "300mm"
    }
  }'

โœ… 2. ํ…์ŠคํŠธ ์ฟผ๋ฆฌ - ์ด๋ฏธ์ง€ ์ปฌ๋ ‰์…˜ ๊ฒ€์ƒ‰ (Tomoro)

๐ŸŸก 2-1. ํ•„ํ„ฐ ์—†์ด ๊ฒ€์ƒ‰

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text/tomoro' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "๋ฌผ๋ฅ˜์—…์ฒด ์„ ์ • ๊ธฐ์ค€",
    "top_k": 3
  }'

๐ŸŸก 2-2. ๋‹ค์ค‘ ํ•„ํ„ฐ ์ ์šฉ

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text/tomoro' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "์ถœํ•˜๊ฒ€์‚ฌ ๊ธฐ์ค€",
    "top_k": 3,
    "filters": {
      "document_type": "์ง€์นจ์„œ",
      "apply_product": "์ง€์›๋ถ€๋ฌธ"
    }
  }'

โœ… 3. ํ…์ŠคํŠธ ์ฟผ๋ฆฌ - ์ด๋ฏธ์ง€ ์ปฌ๋ ‰์…˜ ๊ฒ€์ƒ‰ (ColQwen3.5)

๐ŸŸข 3-1. ํ•„ํ„ฐ ์—†์ด ๊ฒ€์ƒ‰

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text/colqwen35' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "์•ˆ์ „์šด์ „ ์ ˆ์ฐจ",
    "top_k": 3
  }'

๐ŸŸข 3-2. ๋‹ค์ค‘ ํ•„ํ„ฐ ์ ์šฉ

curl -X 'POST' \
  'http://10.150.6.47:4275/search/text/colqwen35' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "query": "๋ฌผ๋ฅ˜์—…์ฒด ์„ ์ • ๊ธฐ์ค€",
    "top_k": 3,
    "filters": {
      "apply_region": "์ „์‚ฌ๊ณตํ†ต",
      "dept_name": "Infra๊ตฌ๋งคํŒ€"
    }
  }'

3. ๋‹จ์–ด ์‚ฌ์ „

๋‹จ์–ด์‚ฌ์ „ ํ˜ธ์ถœํ•˜๋Š” ๋ฒ•

curl -X POST 'http://10.150.6.47:4276/search/term' \
-H 'Content-Type: application/json' \
-d '{
  "query": "์•ˆ์ „๊ณจ๋“ ๋ฃฐ",
  "top_k": 3,
  "filters": {
    "apply_region": "์ „์‚ฌ๊ณตํ†ต",
    "document_type": "์ง€์นจ์„œ"
  }
}'

4. ์ตœ์ข… api call

import requests
import json

# ๐Ÿ”ง ์„ค์ •
BASE_URL = "http://10.150.6.46:18504"  # FastAPI ์„œ๋ฒ„ ์ฃผ์†Œ
SEARCH_ENDPOINT = f"{BASE_URL}/search"

# ๐Ÿ“ ์ฟผ๋ฆฌ ์š”์ฒญ ๋ฐ์ดํ„ฐ
payload = {
    "query": "TTV ๊ทœ๊ฒฉ ๊ธฐ์ค€",
    "top_k": 10,
    "image_backend": "colqwen35",  # ๋˜๋Š” "tomoro"
    "alpha_override": None,  # ์ž๋™์œผ๋กœ query_type ๊ธฐ๋ฐ˜ alpha ์ ์šฉ
    "filters": {
        # ํ•„์š” ์‹œ ํ•„ํ„ฐ ์ถ”๊ฐ€ (์˜ˆ: ๋ถ€์„œ, ์ง€์—ญ, ์ œํ’ˆ ๋“ฑ)
        # "dept_name": "ํ’ˆ์งˆ๊ด€๋ฆฌํŒ€",
        # "apply_region": "Korea"
    },
    "return_debug": True,   # ๋””๋ฒ„๊ทธ ์ •๋ณด ํฌํ•จ (๊ฐœ๋ฐœ์šฉ)
    "rerank": True          # ๋ฆฌ๋žญ์ปค ์‚ฌ์šฉ ์—ฌ๋ถ€ (ํ™˜๊ฒฝ ๊ธฐ๋ณธ๊ฐ’ ๋”ฐ๋ฆ„)
}

# ๐ŸŒ ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
try:
    print("๐Ÿ” 'TTV ๊ทœ๊ฒฉ ๊ธฐ์ค€' ์ฟผ๋ฆฌ ์ „์†ก ์ค‘...")
    response = requests.post(
        SEARCH_ENDPOINT,
        json=payload,
        timeout=60  # ์„œ๋ฒ„ ์‘๋‹ต ๋Œ€๊ธฐ ์‹œ๊ฐ„
    )
    
    # ๐Ÿ” ์‘๋‹ต ํ™•์ธ
    if response.status_code == 200:
        result = response.json()
        print("\nโœ… ์„ฑ๊ณต! ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ:")
        print(json.dumps(result, indent=2, ensure_ascii=False))
        
        # ๊ฐ„๋‹จํžˆ ์ƒ์œ„ 3๊ฐœ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        print("\n๐Ÿ“Œ ์ƒ์œ„ 3๊ฐœ ๊ฒฐ๊ณผ ์š”์•ฝ:")
        for i, item in enumerate(result["results"][:3], 1):
            rank = item["rank"]
            score = item["score"]
            rerank_score = item.get("rerank_score")
            content = item["content"] or "(๋‚ด์šฉ ์—†์Œ)"
            print(f"  {i}. [Rank:{rank}] (Score: {score:.4f}, Rerank: {rerank_score})")
            print(f"     {content[:150]}...")
    else:
        print(f"โŒ ์š”์ฒญ ์‹คํŒจ: {response.status_code} - {response.text}")

except requests.exceptions.ConnectionError:
    print("๐Ÿšจ ์—ฐ๊ฒฐ ์˜ค๋ฅ˜: FastAPI ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰ ์ค‘์ธ์ง€ ํ™•์ธํ•˜์„ธ์š” (uvicorn ์‹คํ–‰ ์—ฌ๋ถ€)")
except requests.exceptions.Timeout:
    print("โฐ ํƒ€์ž„์•„์›ƒ: ์„œ๋ฒ„ ์‘๋‹ต์ด ์ง€์—ฐ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.")
except Exception as e:
    print(f"๐Ÿ’ฅ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜: {e}")

VLM API CALL

### H200-2 ์„œ๋ฒ„ : Qwen3.5-397B-A17B-FP8
### ์ด๋ฏธ์ง€ Input

import requests
import base64
import mimetypes

'''
Thinking mode: temperature=0.6, top_p=0.95, top_k=20, min_p=0.0, presence_penalty=0.0, repetition_penalty=1.0
Instruct (or non-thinking) mode: temperature=0.7, top_p=0.8, top_k=20, min_p=0.0, presence_penalty=1.5, repetition_penalty=1.0
'''

def encode_image_to_base64(image_path):
    mime_type,_ = mimetypes.guess_type(image_path)
    with open(image_path, "rb") as image_file:
        base64_encoded = base64.b64encode(image_file.read()).decode("utf-8")

    return f"data:{mime_type};base64,{base64_encoded}"

image_path = "./test.png"

image_input =  {
    "type": "image_url",
    "image_url": {
        "url": f"{encode_image_to_base64(image_path)}"
    }
}

response = requests.post(
    "http://10.150.6.160:8000/v1/chat/completions",
    json={
        "model": "Qwen3.5-397B-A17B-FP8",
        'messages':[
            {"role":"system", "content":"๋„ˆ๋Š” SK์‹คํŠธ๋ก ์˜ AI Assistant์ž…๋‹ˆ๋‹ค."},
            {"role":"user", "content":[
                    image_input,
                    {"type":"text","text":"์ด๋ฏธ์ง€ OCRํ•ด์ค˜"}
                ]
            }
        ],
        "temperature":0.6,
        "top_p":0.95,
        "top_k":20,
        "min_p":0.0,
        "presence_penalty":0.0,
        "repetition_penalty":1.0,
    }
)


print(response.json()['choices'][0]['message']['content'])


### Non-thinking
response = requests.post(
    "http://10.150.6.160:8000/v1/chat/completions",
    json={
        "model": "Qwen3.5-397B-A17B-FP8",
        'messages':[
            {"role":"system", "content":"๋„ˆ๋Š” SK์‹คํŠธ๋ก ์˜ AI Assistant์ž…๋‹ˆ๋‹ค."},
            {"role":"user", "content":[
                    {"type":"text","text":"์•ˆ๋…•ํ•˜์„ธ์š”."}
                ]
            }
        ],
        "temperature":0.7,
        "top_p":0.8,
        "top_k":20,
        "min_p":0.0,
        "presence_penalty":1.5,
        "repetition_penalty":1.0,
        "chat_template_kwargs": {"enable_thinking": False} ## Thinking(Reasoning) off
    }
)

print(response.json()['choices'][0]['message']['content'])