-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_llm_simple.py
More file actions
150 lines (125 loc) · 5.2 KB
/
Copy pathtest_llm_simple.py
File metadata and controls
150 lines (125 loc) · 5.2 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Simple script to test LLM with a quick query.
This is a minimal test to verify LLM works before running full validation.
Usage: python test_llm_simple.py
"""
import sys
def test_llm_simple():
"""Test LLM with a simple query"""
print("Testing LLM with simple query...")
print("=" * 60)
try:
from langchain_ollama import OllamaLLM
print("Initializing LLM...")
llm = OllamaLLM(model="mistral:7b")
print("✓ LLM initialized")
print("\nSending test query: 'Say hello if you can read this.'")
response = llm.invoke("Say hello if you can read this.")
if response is None:
print("❌ LLM returned None")
return False
elif len(response.strip()) == 0:
print("❌ LLM returned empty response")
return False
else:
print(f"✓ LLM responded successfully!")
print(f"\nResponse: {response}")
return True
except ImportError as e:
print(f"❌ Failed to import OllamaLLM: {e}")
print("Install with: pip install langchain-ollama")
return False
except Exception as e:
print(f"❌ LLM test failed: {e}")
print("\nMake sure:")
print(" 1. Ollama is running")
print(" 2. mistral:7b model is installed (run: ollama pull mistral:7b)")
return False
def test_rag_chain_simple():
"""Test RAG chain with a simple query"""
print("\n" + "=" * 60)
print("Testing RAG chain with simple query...")
print("=" * 60)
try:
# Import app components
from langchain_ollama import OllamaLLM, OllamaEmbeddings
from langchain_community.document_loaders import CSVLoader, TextLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
print("Loading documents...")
loaders = [
CSVLoader('data/simulated_logs.csv'),
TextLoader('data/telco_manual.txt', encoding='utf-8'),
]
docs = []
for loader in loaders:
docs.extend(loader.load())
print(f"✓ Loaded {len(docs)} documents")
print("Splitting documents...")
text_splitter = CharacterTextSplitter(chunk_size=400, chunk_overlap=40)
split_docs = text_splitter.split_documents(docs)
print(f"✓ Split into {len(split_docs)} chunks")
print("Creating embeddings and vector store...")
embeddings = OllamaEmbeddings(model="mistral:7b")
vectorstore = FAISS.from_documents(split_docs, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
print("✓ Vector store created")
print("Creating RAG chain...")
llm = OllamaLLM(model="mistral:7b")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
prompt = ChatPromptTemplate.from_template(
"You are a telco AI assistant. Use this context:\n{context}\n\n"
"Question: {question}\nAnswer briefly."
)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
print("✓ RAG chain created")
print("\nTesting with query: 'What is network troubleshooting?'")
response = rag_chain.invoke("What is network troubleshooting?")
if response is None or len(response.strip()) == 0:
print("❌ RAG chain returned empty response")
return False
else:
print(f"✓ RAG chain works!")
print(f"\nResponse:\n{response}")
return True
except Exception as e:
print(f"❌ RAG chain test failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
print("\n" + "=" * 60)
print("SIMPLE LLM TEST")
print("=" * 60 + "\n")
# Test 1: Simple LLM
llm_ok = test_llm_simple()
if not llm_ok:
print("\n❌ LLM test failed. Fix issues before testing RAG chain.")
sys.exit(1)
# Test 2: RAG chain (optional, can be skipped if data not ready)
try:
rag_ok = test_rag_chain_simple()
if rag_ok:
print("\n" + "=" * 60)
print("✅ All tests passed! LLM is working correctly.")
print("=" * 60)
sys.exit(0)
else:
print("\n❌ RAG chain test failed.")
sys.exit(1)
except FileNotFoundError as e:
print(f"\n⚠️ RAG chain test skipped: {e}")
print("Run data validation first: python validate_data.py")
sys.exit(0)
except Exception as e:
print(f"\n❌ RAG chain test failed: {e}")
sys.exit(1)