shibbir24 commited on
Commit
2cef258
·
verified ·
1 Parent(s): 2d7013d

Update tool_handler.py

Browse files
Files changed (1) hide show
  1. tool_handler.py +23 -12
tool_handler.py CHANGED
@@ -51,18 +51,28 @@ def chat_memory_tool(memory: str, model="llama-3.3-70b-versatile", use_rag=True)
51
  def treatment_tool(query: str, model="llama-3.3-70b-versatile", use_rag=True) -> str:
52
  try:
53
  query_embedding = get_embedding(query)
54
- results = discharge_collection.query(
55
- query_embeddings=[query_embedding],
56
- n_results=5,
57
- include=["documents"]
58
- )
59
- top_docs = results['documents'][0] if use_rag else []
60
- combined_context = "\n\n".join(top_docs) if use_rag else ""
61
- prompt = (
62
- "You are a helpful medical assistant. Based on the following discharge notes, recommend essential treatment.\n\n"
63
- f"### Notes:\n{combined_context}\n\n### Condition:\n{query}"
64
- if use_rag else f"Patient condition: {query}. What treatment is recommended?"
65
- )
 
 
 
 
 
 
 
 
 
 
66
  response = client.chat.completions.create(
67
  model=model,
68
  messages=[
@@ -71,6 +81,7 @@ def treatment_tool(query: str, model="llama-3.3-70b-versatile", use_rag=True) ->
71
  ]
72
  )
73
  return response.choices[0].message.content
 
74
  except Exception as e:
75
  return f"Error: {str(e)}"
76
 
 
51
  def treatment_tool(query: str, model="llama-3.3-70b-versatile", use_rag=True) -> str:
52
  try:
53
  query_embedding = get_embedding(query)
54
+
55
+ # If RAG is on, query vector DB and prepare context
56
+ if use_rag:
57
+ results = discharge_collection.query(
58
+ query_embeddings=[query_embedding],
59
+ n_results=5,
60
+ include=["documents"]
61
+ )
62
+
63
+ top_docs = results['documents'][0] if results and results['documents'] else []
64
+
65
+ # Truncate each doc to avoid token overflow
66
+ top_docs = [doc[:1500] for doc in top_docs]
67
+ combined_context = "\n\n".join(top_docs)
68
+
69
+ prompt = (
70
+ "You are a helpful medical assistant. Based on the following discharge notes, recommend essential treatment.\n\n"
71
+ f"### Notes:\n{combined_context}\n\n### Condition:\n{query}"
72
+ )
73
+ else:
74
+ prompt = f"Patient condition: {query}. What treatment is recommended?"
75
+
76
  response = client.chat.completions.create(
77
  model=model,
78
  messages=[
 
81
  ]
82
  )
83
  return response.choices[0].message.content
84
+
85
  except Exception as e:
86
  return f"Error: {str(e)}"
87