์ฝ˜ํ…์ธ ๋กœ ์ด๋™

ADK ์—์ด์ „ํŠธ๋ฅผ ์œ„ํ•œ Ollama ๋ชจ๋ธ ํ˜ธ์ŠคํŠธ

ADK์—์„œ ์ง€์›Python v0.1.0

Ollama๋Š” ์˜คํ”ˆ ์†Œ์Šค ๋ชจ๋ธ์„ ๋กœ์ปฌ์—์„œ ํ˜ธ์ŠคํŒ…ํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ADK๋Š” LiteLLM ๋ชจ๋ธ ์ปค๋„ฅํ„ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด Ollama์—์„œ ํ˜ธ์ŠคํŒ…๋˜๋Š” ๋ชจ๋ธ๊ณผ ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ

Ollama์—์„œ ํ˜ธ์ŠคํŒ…๋˜๋Š” ๋ชจ๋ธ๋กœ ์—์ด์ „ํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” LiteLLM ๋ž˜ํผ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ๋Š” Gemma ์˜คํ”ˆ ๋ชจ๋ธ์„ ์—์ด์ „ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ๊ตฌํ˜„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

root_agent = Agent(
    model=LiteLlm(model="ollama_chat/gemma3:latest"),
    name="dice_agent",
    description=(
        "hello world agent that can roll a dice of 8 sides and check prime"
        " numbers."
    ),
    instruction="""
      You roll dice and answer questions about the outcome of the dice rolls.
    """,
    tools=[
        roll_die,
        check_prime,
    ],
)

๊ฒฝ๊ณ : ollama_chat ์ธํ„ฐํŽ˜์ด์Šค ์‚ฌ์šฉ

ollama ๋Œ€์‹  ๋ฐ˜๋“œ์‹œ ollama_chat ํ”„๋กœ๋ฐ”์ด๋”๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ollama๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋„๊ตฌ ํ˜ธ์ถœ ๋ฌดํ•œ ๋ฃจํ”„ ๋˜๋Š” ์ด์ „ ์ปจํ…์ŠคํŠธ ๋ฌด์‹œ ๊ฐ™์€ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๋™์ž‘์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

OLLAMA_API_BASE ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์‚ฌ์šฉ

์ƒ์„ฑ ์š”์ฒญ์—์„œ api_base ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, v1.65.5์—์„œ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋‹ค๋ฅธ API ํ˜ธ์ถœ์— ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์š”์ฒญ์ด ์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ๋กœ๋กœ ๋ผ์šฐํŒ…๋˜๋„๋ก Ollama ์„œ๋ฒ„ URL์— ๋Œ€ํ•œ OLLAMA_API_BASE๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

export OLLAMA_API_BASE="http://localhost:11434"
adk web

๋ชจ๋ธ ์„ ํƒ

์—์ด์ „ํŠธ์—์„œ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ Ollama ์›น์‚ฌ์ดํŠธ์—์„œ ๋„๊ตฌ ์ง€์› ๋ชจ๋ธ์„ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ˆ์ •์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋„๊ตฌ๊ฐ€ ์ง€์›๋˜๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ๋ชจ๋ธ์˜ ๋„๊ตฌ ์ง€์› ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ollama show mistral-small3.1
  Model
    architecture        mistral3
    parameters          24.0B
    context length      131072
    embedding length    5120
    quantization        Q4_K_M

  Capabilities
    completion
    vision
    tools

Capabilities ํ•ญ๋ชฉ์— tools๊ฐ€ ํ‘œ์‹œ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์‚ฌ์šฉํ•˜๋Š” ํ…œํ”Œ๋ฆฟ์„ ํ™•์ธํ•ด ํ•„์š”์— ๋งž๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ollama show --modelfile llama3.2 > model_file_to_modify

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ๋ชจ๋ธ์˜ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋ธ์ด ํ•ญ์ƒ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ๋ฌดํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Given the following functions, please respond with a JSON for a function call
with its proper arguments that best answers the given prompt.

Respond in the format {"name": function name, "parameters": dictionary of
argument name and its value}. Do not use variables.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋” ์„ค๋ช…์ ์ธ ํ”„๋กฌํ”„ํŠธ๋กœ ๊ต์ฒดํ•˜๋ฉด ๋ฌดํ•œ ํ˜ธ์ถœ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Review the user's prompt and the available functions listed below.

First, determine if calling one of these functions is the most appropriate way
to respond. A function call is likely needed if the prompt asks for a specific
action, requires external data lookup, or involves calculations handled by the
functions. If the prompt is a general question or can be answered directly, a
function call is likely NOT needed.

If you determine a function call IS required: Respond ONLY with a JSON object in
the format {"name": "function_name", "parameters": {"argument_name": "value"}}.
Ensure parameter values are concrete, not variables.

If you determine a function call IS NOT required: Respond directly to the user's
prompt in plain text, providing the answer or information requested. Do not
output any JSON.

๊ทธ๋Ÿฐ ๋‹ค์Œ ์•„๋ž˜ ๋ช…๋ น์œผ๋กœ ์ƒˆ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ollama create llama3.2-modified -f model_file_to_modify

OpenAI ํ”„๋กœ๋ฐ”์ด๋” ์‚ฌ์šฉ

๋Œ€์‹  openai๋ฅผ ํ”„๋กœ๋ฐ”์ด๋” ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ OLLAMA_API_BASE ๋Œ€์‹  ๋‹ค์Œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. - OPENAI_API_BASE=http://localhost:11434/v1 - OPENAI_API_KEY=anything

์ฐธ๊ณ : API_BASE ๊ฐ’ ๋’ค์— "/v1"์ด ๋ถ™์Šต๋‹ˆ๋‹ค.

root_agent = Agent(
    model=LiteLlm(model="openai/mistral-small3.1"),
    name="dice_agent",
    description=(
        "hello world agent that can roll a dice of 8 sides and check prime"
        " numbers."
    ),
    instruction="""
      You roll dice and answer questions about the outcome of the dice rolls.
    """,
    tools=[
        roll_die,
        check_prime,
    ],
)
export OPENAI_API_BASE=http://localhost:11434/v1
export OPENAI_API_KEY=anything
adk web

๋””๋ฒ„๊น…

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž„ํฌํŠธ ์งํ›„์— ์ถ”๊ฐ€ํ•˜๋ฉด Ollama ์„œ๋ฒ„๋กœ ์ „์†ก๋˜๋Š” ์š”์ฒญ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import litellm
litellm._turn_on_debug()

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์˜ ๋ผ์ธ์„ ํ™•์ธํ•˜์„ธ์š”.

Request Sent from LiteLLM:
curl -X POST \
http://localhost:11434/api/chat \
-d '{'model': 'mistral-small3.1', 'messages': [{'role': 'system', 'content': ...