Skip to content

Commit

Permalink
refactor: Add support for multiple language models in get_response_fr…
Browse files Browse the repository at this point in the history
…om_llm function
  • Loading branch information
preetvadaliya committed Jul 16, 2024
1 parent ed75add commit c1df437
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
21 changes: 16 additions & 5 deletions functions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from chain import create_health_ai_chain
from config import initialize_firebase
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI
from store import get_vector_store

Expand All @@ -16,10 +18,19 @@ def get_health_ai_response(question, llm):


def get_response_from_llm(query, llm):
models = {'gpt-4': {}, 'gpt-3.5-turbo-instruct': {'name': 'gpt-3.5-turbo-instruct'}}
if llm in models:
llm_model = ChatOpenAI(api_key=environ.get('OPEN_AI_API_KEY'), temperature=0, **models[llm])
if llm == 'gpt-4' or llm == 'gpt-3.5-turbo-instruct':
llm_model = ChatOpenAI(api_key=environ.get('OPEN_AI_API_KEY'), temperature=0, model=llm)
response = get_health_ai_response(query, llm_model)
elif llm == 'gemini':
llm_model = ChatGoogleGenerativeAI(
model='gemini-1.5-pro-latest', google_api_key=environ.get('GOOGLE_API_KEY')
)
response = get_health_ai_response(query, llm_model)
elif llm == 'claude':
llm_model = ChatAnthropic(
model='claude-3-5-sonnet-20240620', api_key=environ.get('ANTHROPIC_API_KEY')
)
response = get_health_ai_response(query, llm_model)
return response
else:
return 'Model Not Found'
response = 'Model Not Found'
return response
2 changes: 1 addition & 1 deletion functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from handlers import get_response_from_llm


@https_fn.on_request(cors=options.CorsOptions(cors_origins=['*']))
@https_fn.on_request()
def get_response_url(req: https_fn.Request) -> https_fn.Response:
query = req.get_json().get('query', '')
llms = req.get_json().get('llms', ['gpt-4'])
Expand Down
7 changes: 7 additions & 0 deletions functions/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ langchain-community
langchain-openai
langchain-astradb
lark
langchain_core
langchain_google_genai
langchain_anthropic
google-auth
google-auth-oauthlib
google-api-python-client
python-dotenv

0 comments on commit c1df437

Please sign in to comment.