-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2dbd75
commit cefd176
Showing
5 changed files
with
156 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,55 @@ | ||
from langgraph.graph import START, StateGraph | ||
from langgraph.graph import START, StateGraph, MessagesState | ||
from langgraph.graph.state import CompiledStateGraph | ||
from langgraph.prebuilt import tools_condition, ToolNode | ||
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage | ||
|
||
from langgraph.graph import MessagesState | ||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage | ||
|
||
import logging | ||
from home.llm.llm import LLM | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LLMGraph: | ||
prompt_text = ''' | ||
You are a helpful AI medical assistant. You should only respond to health-related topics such as: | ||
- General human health and lifestyle inquiries. | ||
- Questions about the patient’s medical condition, medication regimen, diet, etc. | ||
- Various requests from the patient to their doctor such as make appointments, modify appointments and medication changes. | ||
You should filter out and ignore any unrelated, sensitive, or controversial topics. | ||
''' | ||
prompt_text = '''You are a helpful AI medical assistant namely Patient Chat and are developed by a software | ||
engineer named Sajed. | ||
You should only respond to health-related topics such as: | ||
- General human health and lifestyle inquiries. | ||
- Questions about men, women and children health | ||
- Questions about the patient's medical condition, medication regimen, diet, etc. | ||
- Various requests from the patient to their doctor such as make appointments, modify appointments and medication changes. | ||
You should filter out and ignore any unrelated, overly sensitive, or controversial topics.''' | ||
|
||
def __init__(self): | ||
self.llm = LLM() | ||
self.graph = self.build_graph() | ||
|
||
|
||
def assistant(self, state: MessagesState): | ||
# Prompt message | ||
sys_msg = SystemMessage(content=self.prompt_text) | ||
return {"messages": [self.llm.llm_with_tools.invoke([sys_msg] + state["messages"])]} | ||
|
||
def build_graph(self) -> CompiledStateGraph: | ||
builder = StateGraph(MessagesState) | ||
|
||
# Define nodes: these do the work | ||
builder.add_node("assistant", self.assistant) | ||
builder.add_node("tools", ToolNode(self.llm.tools)) | ||
|
||
# Define edges: these determine how the control flow moves | ||
builder.add_edge(START, "assistant") | ||
builder.add_conditional_edges( | ||
"assistant", | ||
# If the latest message (result) from assistant is a tool call -> tools_condition routes to tools | ||
# If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END | ||
tools_condition, | ||
) | ||
builder.add_conditional_edges("assistant", tools_condition) | ||
builder.add_edge("tools", "assistant") | ||
|
||
return builder.compile() | ||
|
||
def inference(self, user_message) -> str: | ||
messages = [HumanMessage(content=user_message)] | ||
messages = self.graph.invoke({"messages": messages}) | ||
for m in messages['messages']: | ||
m.pretty_print() | ||
def inference(self, user_message, history) -> str: | ||
messages = [] | ||
for msg in history: | ||
if msg['role'] == 'user': | ||
messages.append(HumanMessage(content=msg['content'])) | ||
elif msg['role'] == 'assistant': | ||
messages.append(AIMessage(content=msg['content'])) | ||
|
||
messages.append(HumanMessage(content=user_message)) | ||
|
||
result = self.graph.invoke({"messages": messages}) | ||
logger.debug(result) | ||
|
||
print() | ||
return messages['messages'][-1].content | ||
return result['messages'][-1].content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
from django.shortcuts import render, HttpResponse | ||
|
||
from django.shortcuts import render | ||
from home.llm.llm_graph import LLMGraph | ||
|
||
|
||
def inference(request): | ||
return HttpResponse(LLMGraph().inference("How to avoid diabetes.")) | ||
from django.http import JsonResponse | ||
from django.views.decorators.csrf import csrf_exempt | ||
import json | ||
|
||
|
||
def index(request): | ||
return render(request, 'index.html') | ||
|
||
|
||
from django.http import JsonResponse | ||
from django.views.decorators.csrf import ensure_csrf_cookie | ||
import json | ||
|
||
|
||
@ensure_csrf_cookie | ||
@csrf_exempt | ||
def inference(request): | ||
if request.method == 'POST': | ||
data = json.loads(request.body) | ||
user_type = data.get('userType') | ||
message = data.get('message') | ||
history = data.get('history') | ||
history = data.get('history', []) | ||
|
||
# Process the message and generate a response | ||
# This is where you'd call your AI model or processing logic | ||
response = LLMGraph().inference(message) | ||
llm_graph = LLMGraph() | ||
response = llm_graph.inference(message, history) | ||
|
||
return JsonResponse({'response': response}) | ||
else: | ||
return JsonResponse({'error': 'Only POST requests are allowed'}, status=405) | ||
return JsonResponse({'error': 'There was an error in the request to the server.'}, status=405) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters