Made llm_api_url optional

Tested with Ollama local and it works without providing any URL.
Also made MINIMUM_WAIT_TIME a config variable in app_config which can easily be tweaked
This commit is contained in:
tapas-joshi 2024-09-13 12:12:42 -04:00
parent 8c7df4c2f9
commit 0534297ccc
3 changed files with 32 additions and 20 deletions

View file

@ -1 +1,4 @@
MINIMUM_LOG_LEVEL="DEBUG" # LOGGING
MINIMUM_LOG_LEVEL = "DEBUG"
MINIMUM_WAIT_TIME = 60 * 15

View file

@ -10,6 +10,7 @@ from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
import src.utils as utils import src.utils as utils
from app_config import MINIMUM_WAIT_TIME
from src.job import Job from src.job import Job
from src.linkedIn_easy_applier import LinkedInEasyApplier from src.linkedIn_easy_applier import LinkedInEasyApplier
from loguru import logger from loguru import logger
@ -78,7 +79,7 @@ class LinkedInJobManager:
searches = list(product(self.positions, self.locations)) searches = list(product(self.positions, self.locations))
random.shuffle(searches) random.shuffle(searches)
page_sleep = 0 page_sleep = 0
minimum_time = 60 * 15 minimum_time = MINIMUM_WAIT_TIME
minimum_page_time = time.time() + minimum_time minimum_page_time = time.time() + minimum_time
for position, location in searches: for position, location in searches:

View file

@ -12,6 +12,7 @@ from typing import Union
import httpx import httpx
from Levenshtein import distance from Levenshtein import distance
from dotenv import load_dotenv from dotenv import load_dotenv
from langchain_core.messages import BaseMessage
from langchain_core.messages.ai import AIMessage from langchain_core.messages.ai import AIMessage
from langchain_core.output_parsers import StrOutputParser from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompt_values import StringPromptValue from langchain_core.prompt_values import StringPromptValue
@ -30,44 +31,50 @@ class AIModel(ABC):
class OpenAIModel(AIModel): class OpenAIModel(AIModel):
def __init__(self, api_key: str, llm_model: str, llm_api_url: str): def __init__(self, api_key: str, llm_model: str):
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
self.model = ChatOpenAI(model_name=llm_model, openai_api_key=api_key, self.model = ChatOpenAI(model_name=llm_model, openai_api_key=api_key,
temperature=0.4, base_url=llm_api_url) temperature=0.4)
def invoke(self, prompt: str) -> str: def invoke(self, prompt: str) -> BaseMessage:
logger.debug("Invoking OpenAI API") logger.debug("Invoking OpenAI API")
response = self.model.invoke(prompt) response = self.model.invoke(prompt)
return response return response
class ClaudeModel(AIModel): class ClaudeModel(AIModel):
def __init__(self, api_key: str, llm_model: str, llm_api_url: str): def __init__(self, api_key: str, llm_model: str):
from langchain_anthropic import ChatAnthropic from langchain_anthropic import ChatAnthropic
self.model = ChatAnthropic(model=llm_model, api_key=api_key, self.model = ChatAnthropic(model=llm_model, api_key=api_key,
temperature=0.4, base_url=llm_api_url) temperature=0.4)
def invoke(self, prompt: str) -> str: def invoke(self, prompt: str) -> BaseMessage:
response = self.model.invoke(prompt) response = self.model.invoke(prompt)
logger.debug("Invoking Claude API")
return response return response
class OllamaModel(AIModel): class OllamaModel(AIModel):
def __init__(self, api_key: str, llm_model: str, llm_api_url: str): def __init__(self, llm_model: str, llm_api_url: str):
from langchain_ollama import ChatOllama from langchain_ollama import ChatOllama
self.model = ChatOllama(model=llm_model, base_url=llm_api_url)
def invoke(self, prompt: str) -> str: if len(llm_api_url) > 0:
logger.debug(f"Using Ollama with API URL: {llm_api_url}")
self.model = ChatOllama(model=llm_model, base_url=llm_api_url)
else:
self.model = ChatOllama(model=llm_model)
def invoke(self, prompt: str) -> BaseMessage:
response = self.model.invoke(prompt) response = self.model.invoke(prompt)
return response return response
class GeminiModel(AIModel): class GeminiModel(AIModel):
def __init__(self, api_key:str, llm_model: str, llm_api_url: str): def __init__(self, api_key:str, llm_model: str):
from langchain_google_genai import ChatGoogleGenerativeAI from langchain_google_genai import ChatGoogleGenerativeAI
self.model = ChatGoogleGenerativeAI(model=llm_model, google_api_key=api_key) self.model = ChatGoogleGenerativeAI(model=llm_model, google_api_key=api_key)
def invoke(self, prompt: str) -> str: def invoke(self, prompt: str) -> BaseMessage:
response = self.model.invoke(prompt) response = self.model.invoke(prompt)
return response return response
@ -79,18 +86,19 @@ class AIAdapter:
def _create_model(self, config: dict, api_key: str) -> AIModel: def _create_model(self, config: dict, api_key: str) -> AIModel:
llm_model_type = config['llm_model_type'] llm_model_type = config['llm_model_type']
llm_model = config['llm_model'] llm_model = config['llm_model']
llm_api_url = config['llm_api_url']
logger.debug('Using {0} with {1} from {2}'.format( llm_api_url = config.get('llm_api_url', "")
llm_model_type, llm_model, llm_api_url))
logger.debug(f"Using {llm_model_type} with {llm_model}")
if llm_model_type == "openai": if llm_model_type == "openai":
return OpenAIModel(api_key, llm_model, llm_api_url) return OpenAIModel(api_key, llm_model)
elif llm_model_type == "claude": elif llm_model_type == "claude":
return ClaudeModel(api_key, llm_model, llm_api_url) return ClaudeModel(api_key, llm_model)
elif llm_model_type == "ollama": elif llm_model_type == "ollama":
return OllamaModel(api_key, llm_model, llm_api_url) return OllamaModel(llm_model, llm_api_url)
elif llm_model_type == "gemini": elif llm_model_type == "gemini":
return GeminiModel(api_key, llm_model, llm_api_url) return GeminiModel(api_key, llm_model)
else: else:
raise ValueError(f"Unsupported model type: {llm_model_type}") raise ValueError(f"Unsupported model type: {llm_model_type}")