David Ko commited on
Commit
e821ec3
ยท
1 Parent(s): d10dea2

Fix: LLM query token length limit issue

Browse files
Files changed (1) hide show
  1. api.py +27 -1
api.py CHANGED
@@ -98,9 +98,24 @@ def process_llm_query(vision_results, user_query):
98
  if llm_model is None or llm_tokenizer is None:
99
  return {"error": "LLM model not available"}
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Create a prompt combining vision results and user query
102
  prompt = f"""You are an AI assistant analyzing image detection results.
103
- Here are the objects detected in the image: {json.dumps(vision_results, indent=2)}
104
 
105
  User question: {user_query}
106
 
@@ -111,6 +126,17 @@ def process_llm_query(vision_results, user_query):
111
  try:
112
  start_time = time.time()
113
 
 
 
 
 
 
 
 
 
 
 
 
114
  inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
115
  with torch.no_grad():
116
  output = llm_model.generate(
 
98
  if llm_model is None or llm_tokenizer is None:
99
  return {"error": "LLM model not available"}
100
 
101
+ # ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ์š”์•ฝ (ํ† ํฐ ๊ธธ์ด ์ œํ•œ์„ ์œ„ํ•ด)
102
+ summarized_results = []
103
+
104
+ # ๊ฐ์ฒด ํƒ์ง€ ๊ฒฐ๊ณผ ์š”์•ฝ
105
+ if isinstance(vision_results, list):
106
+ # ์ตœ๋Œ€ 10๊ฐœ ๊ฐ์ฒด๋งŒ ํฌํ•จ
107
+ for i, obj in enumerate(vision_results[:10]):
108
+ if isinstance(obj, dict):
109
+ # ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ์ถ”์ถœ
110
+ summary = {
111
+ "label": obj.get("label", "unknown"),
112
+ "confidence": obj.get("confidence", 0),
113
+ }
114
+ summarized_results.append(summary)
115
+
116
  # Create a prompt combining vision results and user query
117
  prompt = f"""You are an AI assistant analyzing image detection results.
118
+ Here are the objects detected in the image: {json.dumps(summarized_results, indent=2)}
119
 
120
  User question: {user_query}
121
 
 
126
  try:
127
  start_time = time.time()
128
 
129
+ # ํ† ํฐ ๊ธธ์ด ํ™•์ธ ๋ฐ ์ œํ•œ
130
+ tokens = llm_tokenizer.encode(prompt)
131
+ if len(tokens) > 1500: # ์•ˆ์ „ ๋งˆ์ง„ ์„ค์ •
132
+ prompt = f"""You are an AI assistant analyzing image detection results.
133
+ The image contains {len(summarized_results)} detected objects.
134
+
135
+ User question: {user_query}
136
+
137
+ Please provide a general analysis based on the user's question.
138
+ """
139
+
140
  inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
141
  with torch.no_grad():
142
  output = llm_model.generate(