Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Ollama Langchain agent #31

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions prediction_market_agent/agents/langchain_agent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from typing import Optional

from langchain.agents import AgentType, initialize_agent, load_tools
from langchain_community.llms import OpenAI
from langchain_core.language_models import BaseLLM
from prediction_market_agent_tooling.markets.agent_market import AgentMarket

from prediction_market_agent import utils
from prediction_market_agent.agents.abstract import AbstractAgent


class LangChainAgent(AbstractAgent):
def __init__(self) -> None:
def __init__(self, llm: Optional[BaseLLM] = None) -> None:
keys = utils.APIKeys()
llm = OpenAI(openai_api_key=keys.openai_api_key)
llm = OpenAI(openai_api_key=keys.openai_api_key) if not llm else llm
# Can use pre-defined search tool
# TODO: Tavily tool could give better results
# https://docs.tavily.com/docs/tavily-api/langchain
Expand Down
17 changes: 17 additions & 0 deletions prediction_market_agent/agents/ollama_langchain_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from langchain_community.llms.ollama import Ollama

from prediction_market_agent.agents.langchain_agent import LangChainAgent
from prediction_market_agent.tools.ollama_utils import is_ollama_running


class OllamaLangChainAgent(LangChainAgent):
def __init__(self) -> None:
# Make sure Ollama is running locally
if not is_ollama_running():
raise EnvironmentError(
"Ollama is not running, cannot instantiate Ollama agent"
)
llm = Ollama(
model="mistral", base_url="http://localhost:11434"
) # Mistral since it supports function calling
super().__init__(llm=llm)
6 changes: 6 additions & 0 deletions prediction_market_agent/tools/ollama_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import requests


def is_ollama_running(base_url: str = "http://localhost:11434") -> bool:
r = requests.get(f"{base_url}/api/tags")
return r.status_code == 200
Comment on lines +4 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The is_ollama_running function correctly checks if the Ollama service is running by making a GET request to the /api/tags endpoint. However, there are a few areas that could be improved:

  1. Error Handling: Currently, the function does not handle exceptions that might occur during the request, such as network errors or timeouts. It's recommended to wrap the request in a try-except block to catch exceptions like requests.exceptions.RequestException.

  2. Performance: Making a synchronous HTTP request could block the execution if the Ollama service is slow to respond or if there are network issues. Consider using asynchronous requests or setting a timeout for the request to improve responsiveness.

  3. Security: While not directly related to the current implementation, ensure that any communication with the Ollama service, especially in production environments, is secured (e.g., using HTTPS).

Suggested improvements:

import requests

def is_ollama_running(base_url: str = "http://localhost:11434") -> bool:
    try:
        r = requests.get(f"{base_url}/api/tags", timeout=5)  # Set a reasonable timeout
        return r.status_code == 200
    except requests.exceptions.RequestException:
        return False

Loading