Python・Langchain・OllamaでエージェンティックAIを構築する方法(eコマース & 工場自動化向け)

AIは単なるチャットボットから進化し、今や「考えて、計画し、行動する」**エージェンティックAI(Agentic AI)**の時代が始まりました。本記事では、以下の技術を使って現実的なAIエージェントを構築します:

  • 🧠 Langchain:ツール利用・メモリ・エージェントロジックのためのフレームワーク
  • 🔓 Ollama:ローカルで動作するオープンソースLLM(例:LLaMA3、Mistral)
  • 🛒 カスタムツール:eコマースと🏭 工場の自動化に対応

🤔 エージェンティックAIとは?

エージェンティックAIは、以下のような機能を持つ自律型AIシステムです:

  • 過去の会話やアクションを覚える「記憶力
  • ゴール達成のためのステップを考える「計画力
  • 外部のツールやAPIを呼び出す「実行力
  • ユーザーの助けなしでも自律的に動作する「自立性

🧰 技術スタック

技術 説明
Python メインのプログラミング言語
Langchain エージェントのフレームワーク
Ollama LLM(例:LLaMA3)をローカルで実行
カスタムツール eコマースや工場向けの機能をシミュレート

🧠 エージェントのアーキテクチャ

graph TD
    A["ユーザーの質問"] --> B["Langchain Agent"]
    B --> C["LLM (Ollama経由)"]
    B --> D["会話メモリ"]
    B --> E["ツール選択"]

    E --> F["search_products()"]
    E --> G["track_order()"]
    E --> H["check_production_status()"]
    E --> I["log_factory_issue()"]

    F --> J["商品データベース"]
    G --> K["注文管理システム"]
    H --> L["工場設備DB"]
    I --> M["メンテナンスログ"]

    C -->|"推論&計画"| B
    D -->|"文脈保持"| B

📦 ステップ1:ツール関数の作成

# tools.py
from langchain.tools import tool

@tool
def search_products(keyword: str) -> str:
    return f"「{keyword}」に一致する商品3件を見つけました:A、B、C。"

@tool
def track_order(order_id: str) -> str:
    return f"注文 {order_id} は配送中です。"

@tool
def check_production_status(machine_id: str) -> str:
    return f"{machine_id} のバッチ #42 は87%完了しています。"

@tool
def log_factory_issue(issue: str) -> str:
    return f"問題「{issue}」を記録し、技術者に割り当てました。"

🤖 ステップ2:Langchain + Ollama でエージェントを構築

# agent.py
from langchain_community.chat_models import ChatOllama
from langchain.agents import initialize_agent, AgentType
from langchain.memory import ConversationBufferMemory
from tools import search_products, track_order, check_production_status, log_factory_issue

llm = ChatOllama(model="llama3")  # 事前に `ollama run llama3` を実行しておく

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

agent = initialize_agent(
    tools=[search_products, track_order, check_production_status, log_factory_issue],
    llm=llm,
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    memory=memory,
    verbose=True
)

💬 ステップ3:エージェントの起動

# main.py
from agent import agent

print("=== eコマース&工場向けエージェンティックAI ===")
while True:
    query = input("あなた: ")
    if query.lower() in ['exit', 'quit']:
        break
    result = agent.run(query)
    print("Agent:", result)

🧪 使用例

あなた: ブルートゥーススピーカーを探して
Agent: 「ブルートゥーススピーカー」に一致する商品3件を見つけました:A、B、C。

あなた: 注文番号99123のステータスは?
Agent: 注文 99123 は配送中です。

あなた: A7機の進捗状況は?
Agent: A7 のバッチ #42 は87%完了しています。

あなた: A7が過熱している問題を記録して
Agent: 問題「A7が過熱している」を記録し、技術者に割り当てました。

✅ なぜエージェンティックAIが重要なのか?

  • より賢く業務を自動化:ただのチャットではなく、目的達成に向けて動くAIを実現
  • コスト効率の良い運用:OllamaとOSSモデルで低コストかつローカル実行
  • 拡張性抜群:機能を自由に追加し、自社のシステムと連携可能
  • プライバシー保護:クラウドに依存せず社内完結で安全

🚀 次にやるべきこと

  • FastAPI または Streamlit でUIを追加
  • 実際のデータベースやAPIと連携
  • ベクトルDBで長期記憶を実装(FAISS, Chromaなど)
  • 複数エージェントの協調動作を試す(CrewAI, LangGraph)

Related Posts

Our Products


Related Posts

Our Products


Get in Touch with us

Speak to Us or Whatsapp(+66) 83001 0222

Chat with Us on LINEiiitum1984

Our HeadquartersChanthaburi, Thailand