File size: 4,730 Bytes
7616a39 9929084 a2f88d9 9929084 a2f88d9 9929084 7616a39 9929084 7616a39 9929084 7616a39 822f946 7616a39 c7aa14e 7616a39 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import os
from langchain.text_splitter import RecursiveCharacterTextSplitter
from typing import Any, Optional
import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
def get_llm(model: str = "gemini-2.5-flash",
temperature: float = 0.0,
max_tokens: Optional[int] = None,
timeout: Optional[int] = None,
max_retries: int = 3) -> Any:
"""
Return a LangChain ChatGoogleGenerativeAI LLM configured to use Gemini.
- Reads GEMINI_API_KEY from environment.
- Default model: 'gemini-2.5-flash' (change if you need another).
- Temperature default 0 for deterministic responses.
- max_tokens/timeout can be None to allow defaults from the underlying client.
Returns:
An instance of langchain.chat_models.ChatGoogleGenerativeAI (or raises informative error).
"""
try:
from langchain_google_genai import ChatGoogleGenerativeAI
except Exception as e:
logger.exception("langchain ChatGoogleGenerativeAI import failed")
raise RuntimeError(
"langchain (with ChatGoogleGenerativeAI) is required but not installed. "
"Install with: pip install 'langchain[google]' or refer to your LangChain version docs."
) from e
# Prefer explicit environment variable or other configured setting
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
msg = "GEMINI_API_KEY environment variable not set. Set it to your Gemini API key."
logger.error(msg)
raise RuntimeError(msg)
# Build client config
try:
llm = ChatGoogleGenerativeAI(
model=model,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout,
max_retries=max_retries,
api_key=api_key,
)
logger.info("Initialized ChatGoogleGenerativeAI LLM (model=%s)", model)
return llm
except TypeError:
# Some langchain versions may accept different parameter names (api_key vs openai_api_key etc.)
# Try a safer fallback with only the most common args.
try:
llm = ChatGoogleGenerativeAI(model=model, temperature=temperature, api_key=api_key)
logger.info("Initialized ChatGoogleGenerativeAI LLM (fallback constructor) model=%s", model)
return llm
except Exception as e:
logger.exception("Failed to create ChatGoogleGenerativeAI instance")
raise RuntimeError(f"Failed to initialize Gemini LLM: {e}") from e
except Exception as e:
logger.exception("Failed to initialize Gemini LLM")
raise RuntimeError(f"Failed to initialize Gemini LLM: {e}") from e
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. Text Splitter (512 tokens per chunk, 100 token overlap)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=100)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. Embeddings Model
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from dotenv import load_dotenv
load_dotenv()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. Embeddings Model (Google Gemini)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
GOOGLE_API_KEY = os.getenv("GEMINI_API_KEY")
if not GOOGLE_API_KEY:
raise ValueError("GOOGLE_API_KEY is not set in environment variables")
embeddings = GoogleGenerativeAIEmbeddings(
model="models/gemini-embedding-001",
google_api_key=GOOGLE_API_KEY
)
|