Spaces:
Sleeping
Sleeping
File size: 1,775 Bytes
5ac1ec2 0731ede 5ac1ec2 0731ede 5ac1ec2 a218c12 | 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 | # Path: src/nl2sql/hf_engine.py
# This module defines the HuggingFace-based engine for generating SQL queries from natural language questions.
import os
from huggingface_hub import InferenceClient
from langchain_core.language_models.llms import LLM
from typing import Any, List, Optional
# Default Model
DEFAULT_MODEL_ID = "defog/llama-3-sqlcoder-8b:featherless-ai"
# Custom LangChain wrapper for HuggingFace Inference API
class HFChatWrapper(LLM):
"""
Custom LLM wrapper for HuggingFace Inference API to maintain compatibility with LangChain's LLM interface.
"""
client: Any
model_id: str
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs: Any) -> str:
completion = self.client.chat.completions.create(
model = self.model_id,
messages = [
{"role": "user", "content": prompt}
],
temperature = 0.0,
max_tokens = 512
)
return completion.choices[0].message.content
@property
def _llm_type(self) -> str:
return "huggingface_inference_client"
# Initialize the HuggingFace endpoint using the InferenceClient
def get_llm(model_id: str = DEFAULT_MODEL_ID):
"""
Initializes the HuggingFace InferenceClient and returns an LLM instance for generating SQL queries.
"""
# Load HuggingFace API token from environment variable
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError("HuggingFace API token not found!")
print(f"Initializing HuggingFace InferenceClient with model: {model_id}")
# Initialize the HuggingFace InferenceClient
client = InferenceClient(api_key=hf_token)
llm = HFChatWrapper(client=client, model_id=model_id)
return llm |