DocsAI 연결로컬 LLM (Ollama·LM Studio·Gemma)

로컬 LLM (Ollama·LM Studio·Gemma)

오프라인이거나 보안상 클라우드 LLM을 쓰기 어려운 경우, Ollama·LM Studio 등에 띄운 로컬 모델에 MCP 클라이언트를 붙여 FindIP를 도구로 사용할 수 있습니다. 모델은 Llama 3.1, Qwen 2.5, Gemma 2 등 도구 호출(tool use)을 지원하는 8B 이상 모델을 권장합니다.

사전 준비

로컬에서 실행 중인 LLM 인스턴스 + FindIP API 키 + tool calling을 지원하는 클라이언트(예: FastMCP, OpenAI Python SDK with custom base URL).

단계별 설정

1

Ollama로 모델 실행

도구 호출을 지원하는 모델을 띄웁니다.
bash
ollama serve
ollama pull llama3.1:8b-instruct-q4_K_M
# 또는: ollama pull qwen2.5:7b-instruct
2

FindIP 호출 함수 정의

OpenAI 호환 API(Ollama는 localhost:11434/v1)에 도구를 등록합니다.
python
import os, requests
from openai import OpenAI

llm = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")

tools = [{
  "type": "function",
  "function": {
    "name": "findip_search",
    "description": "한·미·일·중·EP 5개국 특허 시맨틱 검색",
    "parameters": {
      "type": "object",
      "properties": {
        "query": {"type": "string"},
        "top_k": {"type": "integer", "default": 10},
      },
      "required": ["query"],
    },
  },
}]

def findip_search(query, top_k=10):
    return requests.post(
        "https://api.findip.ai/api/v1/search/semantic",
        headers={"X-API-Key": os.environ["FINDIP_API_KEY"]},
        json={"query": query, "top_k": top_k},
        timeout=30,
    ).json()
3

도구 호출 루프

모델이 tool_calls를 반환하면 실제 호출 후 결과를 다시 모델에 전달합니다.
python
messages = [{"role": "user", "content": "전고체 배터리 고체 전해질 핵심 특허 5건"}]
res = llm.chat.completions.create(model="llama3.1:8b-instruct-q4_K_M", messages=messages, tools=tools)

if res.choices[0].message.tool_calls:
    for tc in res.choices[0].message.tool_calls:
        result = findip_search(**eval(tc.function.arguments))
        messages.append(res.choices[0].message)
        messages.append({"role": "tool", "tool_call_id": tc.id, "content": str(result)})
    final = llm.chat.completions.create(model="llama3.1:8b-instruct-q4_K_M", messages=messages, tools=tools)
    print(final.choices[0].message.content)

첫 검색 예시

아래 문장을 그대로 LLM에게 보내보세요.

페로브스카이트 태양전지의 안정성 향상 분야 핵심 특허 5건을 한국·미국 위주로 추려서 청구항 요약과 함께 정리해줘.

트러블슈팅

모델이 도구 호출을 하지 않습니다.
도구 호출을 지원하는 모델인지 확인하세요. Llama 3.1 8B/70B Instruct, Qwen 2.5 7B 이상, Gemma 2 27B 등이 안정적입니다. 7B 미만은 실패율이 높습니다.
응답이 너무 느립니다.
로컬 모델이 직접 데이터를 읽고 요약하면 부담이 큽니다. FindIP 응답에서 title·abstract만 추려서 모델에 전달하면 토큰 소모가 1/5 수준으로 줄어듭니다.
MCP 표준을 그대로 쓰고 싶습니다.
FastMCP나 LangChain의 MCP adapter로 https://api.findip.ai/mcp에 직접 연결할 수도 있습니다. 다만 OAuth 흐름은 헤드리스 환경에서 추가 작업이 필요하므로 API Key 방식이 더 단순합니다.
FindIP | Semantic Patent Search