|
|
from langchain_groq import ChatGroq |
|
|
from langchain_openai import ChatOpenAI |
|
|
from sentence_transformers import SentenceTransformer |
|
|
from huggingface_hub import InferenceClient |
|
|
from huggingface_hub import login |
|
|
from dotenv import load_dotenv |
|
|
load_dotenv() |
|
|
import os |
|
|
import requests |
|
|
import numpy as np |
|
|
|
|
|
os.environ['HUGGINGFACEHUB_ACCESS_TOKEN']=os.getenv('HUGGINGFACEHUB_ACCESS_TOKEN') |
|
|
login(os.environ['HUGGINGFACEHUB_ACCESS_TOKEN']) |
|
|
os.environ['GROQ_API_KEY']=os.getenv('GROQ_API_KEY') |
|
|
|
|
|
|
|
|
llm = ChatGroq( |
|
|
model="llama-3.3-70b-versatile", |
|
|
temperature=0.3, |
|
|
max_tokens=500, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HFEmbeddingAPI: |
|
|
def __init__(self, api_url, token): |
|
|
self.api_url = api_url |
|
|
self.headers = { |
|
|
"Authorization": f"Bearer {os.environ.get('HUGGINGFACEHUB_ACCESS_TOKEN')}", |
|
|
} |
|
|
|
|
|
def encode(self, texts): |
|
|
if isinstance(texts, str): |
|
|
texts = [texts] |
|
|
response = requests.post( |
|
|
self.api_url, |
|
|
headers=self.headers, |
|
|
json={"inputs": texts} |
|
|
) |
|
|
response.raise_for_status() |
|
|
embeddings=response.json() |
|
|
return np.array(embeddings[0]) if len(embeddings) == 1 else np.array(embeddings) |
|
|
|
|
|
|
|
|
ST = HFEmbeddingAPI( |
|
|
api_url="https://router.huggingface.co/hf-inference/models/mixedbread-ai/mxbai-embed-large-v1/pipeline/feature-extraction", |
|
|
token=os.environ.get('HUGGINGFACEHUB_ACCESS_TOKEN') |
|
|
) |
|
|
|