amplify / backend /llmoperations.py
github-actions
Sync from GitHub Fri Dec 26 12:29:52 UTC 2025
aff341e
import os
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from pathlib import Path
from functools import lru_cache
from langgraph.prebuilt import create_react_agent
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
load_dotenv()
@lru_cache(maxsize=1)
def get_chat_model():
llm = ChatOpenAI(
model=os.getenv("OPENAI_MODEL"),
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0,
max_tokens = 10000 # Adjust max tokens as needed
)
return llm
@lru_cache(maxsize=1)
def get_local_chat_model():
"""
Return an Ollama-backed ChatOpenAI model (OpenAI compatible endpoint).
Requires Ollama running locally: https://ollama.com
Example: ollama run llama3.1
"""
# model_name = model or os.getenv("OLLAMA_MODEL", "llama3.1")
llm = ChatOpenAI(
model="llama3.2:3b",
base_url="http://localhost:11434/v1",
api_key="ollama", # Placeholder; Ollama ignores this but LangChain expects a key.
temperature=0,
max_tokens=2048,
)
return llm
# def generate_response(user_input: str) -> str:
# system_message = SystemMessage(content="You are a helpful assistant.")
# human_message = HumanMessage(content=f"Please answer to the user query: {user_input}")
# chat_model = get_chat_model()
# response = chat_model.invoke([system_message, human_message])
# print(response)
# return response.content
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
def get_agent_response(user_input: str) -> str:
agent = create_react_agent(
model=get_chat_model(),
tools=[get_weather],
)
response = agent.invoke({"messages": [HumanMessage(user_input)]})
print(response)
return response['messages'][-1].content