-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm.py
157 lines (127 loc) · 5.7 KB
/
llm.py
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
151
152
153
154
155
156
157
import time
import os
import requests
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_openai import ChatOpenAI
from langchain_ollama import ChatOllama
from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph
load_dotenv()
class LanguageModelProcessor:
model_type = None
model_name = None
memory = None
workflow = None
app = None
llm = None
memory = None
def __init__(self, type=None, model_name=None):
llm_mapping = {
'groq': ChatGroq,
'ollama': ChatOllama,
'openai': ChatOpenAI
}
model_names = {
'ollama': [],
'groq': ['llama-3.1-8b-instant', 'llama-3.1-70b-versatile'],
'openai': ['gpt-4o', 'gpt-4o-mini']
}
# Select the LLM type:
if type is not None:
model_type = type
llm_class = llm_mapping.get(model_type)
else:
print("Select the LLM model type:")
for i, model_type in enumerate(llm_mapping.keys(), start=1):
print(f"{i}. {model_type}")
model_type_index = int(input("Enter the number of your choice: ")) - 1
model_type = list(llm_mapping.keys())[model_type_index]
llm_class = llm_mapping.get(model_type)
if llm_class is None:
raise ValueError(f'Invalid model type: {model_type}')
if model_type == 'ollama':
# Fetch model names dynamically
ollama_models = self.fetch_ollama_models()
# Update the model_names dictionary with the fetched models
model_names['ollama'] = ollama_models if ollama_models else ['default_model_name']
if model_name is None:
print(f"Select the {model_type} model:")
for i, model_name in enumerate(model_names[model_type], start=1):
print(f"{i}. {model_name}")
model_name_index = int(input("Enter the number of your choice: ")) - 1
model_name = model_names[model_type][model_name_index]
llm_class = llm_mapping.get(model_type)
# Debugging: Print model_type and model_name
print(f"Model Type: {model_type}, Model Name: {model_name}")
# Initialize the chat model
self.llm = llm_class(model=model_name, base_url=os.getenv("OLLAMA_BASE_URL")) if model_type == 'ollama' else \
llm_class(temperature=0, model_name=model_name, groq_api_key=os.getenv("GROQ_API_KEY")) if model_type == 'groq' else \
llm_class(temperature=0, model_name=model_name, openai_api_key=os.getenv("OPENAI_API_KEY"))
# Define the graph state to be a list of messages
self.workflow = StateGraph(state_schema=MessagesState)
# Define the function that calls the model
def call_model(state: MessagesState):
response = self.llm.invoke(state["messages"])
return {"messages": response}
# Define the (single) node in the graph
self.workflow.add_edge(START, "model")
self.workflow.add_node("model", call_model)
# Add memory
self.memory = MemorySaver()
self.app = self.workflow.compile(checkpointer=self.memory)
# Load the system prompt from a file
with open('system_prompt3.txt', 'r') as file:
system_prompt = file.read().strip()
system_message = SystemMessage(system_prompt)
config = {"configurable": {"thread_id": "default_thread"}}
self.app.update_state(config, {"messages": [system_message]})
'''
self.prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template(system_prompt),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{text}")
])
self.conversation = LLMChain(
llm=self.llm,
prompt=self.prompt,
memory=self.memory
)
'''
def fetch_ollama_models(self):
try:
response = requests.get(os.getenv("OLLAMA_BASE_URL") + "/api/tags")
response.raise_for_status() # Raise an error for bad responses
data = response.json()
# Extract model names from the response
return [model['name'] for model in data.get('models', [])]
except requests.exceptions.RequestException as e:
print(f"Error fetching models from Ollama API: {e}")
return []
def process(self, text):
# Prepare input messages
input_messages = [HumanMessage(text)]
config = {"configurable": {"thread_id": "default_thread"}}
# Invoke the application
output = self.app.invoke({"messages": input_messages}, config)
response_message = output["messages"][-1]
# Print the response
# print(f">> LLM: {response_message.pretty_print()}")
return response_message.content
def reset(self):
# Create empty checkpoint
checkpoint = self.memory.empty_checkpoint()
# Load the system prompt from file
with open('system_prompt3.txt', 'r') as file:
system_prompt = file.read().strip()
system_message = SystemMessage(system_prompt)
# Reset the state with just the system message
config = {"configurable": {"thread_id": "default_thread"}}
self.app.update_state(config, {"messages": [system_message]})
# Update memory with empty checkpoint
self.memory.put(
config=config,
checkpoint=checkpoint,
metadata={}
)