AI_Research_Assistant / withoutllama
Subha95's picture
Create withoutllama
bd08373 verified
import os
from langchain_community.tools import WikipediaQueryRun, ArxivQueryRun
from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper
from langchain_huggingface import HuggingFacePipeline
from langchain.agents import initialize_agent, AgentType
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
from huggingface_hub import login
import torch
import traceback
# βœ… Login to HF
token = os.getenv("HF_TOKEN")
print("πŸ”‘ HF_TOKEN available?", token is not None)
if token:
login(token=token)
else:
print("❌ No HF_TOKEN found in environment")
def build_qa():
print("πŸš€ Starting QA pipeline...")
# ---- 1. Tools ----
try:
print("πŸ”Ή Initializing Wikipedia tool...")
wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
print("πŸ”Ή Initializing Arxiv tool...")
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
tools = [wiki, arxiv]
print("βœ… Tools initialized")
except Exception as e:
print("❌ Tools initialization failed:", e)
traceback.print_exc()
return None
# ---- 2. Model ----
try:
print("πŸ”Ή Loading Mistral 7B model...")
model_name = "mistralai/Mistral-7B-Instruct-v0.3" # or your CPU-quantized version
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto", # works after installing accelerate
dtype=torch.float16, # instead of torch_dtype
)
llm = TextGenerationPipeline(
model=model,
tokenizer=tokenizer,
max_new_tokens=256,
temperature=0.2,
do_sample=False,
top_p=0.9,
repetition_penalty=1.2,
eos_token_id=tokenizer.eos_token_id,
return_full_text=False,
)
hf_llm = HuggingFacePipeline(pipeline=llm)
print(f"βœ… Model loaded: {model_name}")
except Exception as e:
print("❌ Model load failed:", e)
traceback.print_exc()
return None
# ---- 3. Agent ----
try:
print("πŸ”Ή Initializing agent...")
agent = initialize_agent(
tools=tools,
llm=hf_llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
handle_parsing_errors=True,
)
print("βœ… Agent initialized")
except Exception as e:
print("❌ Agent initialization failed:", e)
traceback.print_exc()
return None
print("βœ… QA pipeline ready")
return agent
# ---- Build once ----
try:
agent = build_qa()
if agent:
print("βœ… QA pipeline built successfully:", type(agent))
else:
print("❌ QA pipeline build returned None")
except Exception as e:
agent = None
print("❌ Failed to build QA pipeline:", e)
traceback.print_exc()
def get_response(user_message, history):
if agent is None:
return "⚠️ QA pipeline not initialized."
try:
print("πŸ’¬ User query:", user_message)
response = agent.invoke({"input": user_message})
print("πŸ€– Agent response:", response)
return response
except Exception as e:
print("❌ Agent execution failed:", e)
traceback.print_exc()
return f"❌ QA run failed: {e}"