Spaces:
Runtime error
Runtime error
Commit ·
43d607a
1
Parent(s): ca716d2
Fixed Reload Button
Browse filesThe Reload button now reloads your suggestions and excludes the previous suggestions giving you new suggestions.
The animation is fixed
- app/main.py +25 -17
app/main.py
CHANGED
|
@@ -12,6 +12,7 @@ load_dotenv() # Load environment variables from .env into os.environ
|
|
| 12 |
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 15 |
from pydantic import BaseModel
|
| 16 |
|
| 17 |
# Import LangChain components
|
|
@@ -298,37 +299,44 @@ def get_llm(model_name: str):
|
|
| 298 |
###############################################################################
|
| 299 |
|
| 300 |
@app.get("/suggestions", response_model=Dict[str, Any])
|
| 301 |
-
async def get_suggestions(
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
"For the following business question, provide exactly 3 concise bullet-point suggestions. "
|
| 305 |
-
"Business idea: \"{business_idea}\". "
|
| 306 |
-
"Question: \"{question}\". "
|
| 307 |
-
"Do not include any extra text.
|
| 308 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
-
# (Initialize and run your LLMChain here as you already do.)
|
| 311 |
-
# For example:
|
| 312 |
try:
|
| 313 |
-
llm = get_llm(model) #
|
| 314 |
suggestion_prompt = PromptTemplate(
|
| 315 |
-
input_variables=["
|
| 316 |
-
template=
|
| 317 |
)
|
| 318 |
suggestion_chain = LLMChain(llm=llm, prompt=suggestion_prompt)
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
)
|
| 323 |
-
|
| 324 |
suggestion_array = [
|
| 325 |
-
re.sub(r'^\s*\d
|
| 326 |
for line in raw_text.split("\n") if line.strip()
|
| 327 |
]
|
| 328 |
if not suggestion_array:
|
| 329 |
suggestion_array = ["No suggestions available"]
|
| 330 |
return {"suggestions": suggestion_array}
|
| 331 |
except Exception as e:
|
|
|
|
| 332 |
raise HTTPException(status_code=500, detail=f"Error generating suggestions: {str(e)}")
|
| 333 |
|
| 334 |
@app.post("/generate")
|
|
|
|
| 12 |
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
+
from fastapi import Query
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
# Import LangChain components
|
|
|
|
| 299 |
###############################################################################
|
| 300 |
|
| 301 |
@app.get("/suggestions", response_model=Dict[str, Any])
|
| 302 |
+
async def get_suggestions(
|
| 303 |
+
model: str = "Mistral",
|
| 304 |
+
business_idea: str = "",
|
| 305 |
+
question: str = "",
|
| 306 |
+
exclude: List[str] = Query([]), # ⬅️ now accepts multiple ?exclude=
|
| 307 |
+
) -> Dict[str, Any]:
|
| 308 |
+
# Construct your base prompt
|
| 309 |
+
prompt = (
|
| 310 |
"For the following business question, provide exactly 3 concise bullet-point suggestions. "
|
| 311 |
+
f"Business idea: \"{business_idea}\". "
|
| 312 |
+
f"Question: \"{question}\". "
|
| 313 |
+
"Do not include any extra text."
|
| 314 |
)
|
| 315 |
+
# If the client provided exclusions, tell the model not to repeat them
|
| 316 |
+
if exclude:
|
| 317 |
+
prompt += " Do not repeat any of these suggestions: " + ", ".join(exclude) + "."
|
| 318 |
+
|
| 319 |
+
prompt += "\nSuggestions:\n-"
|
| 320 |
|
|
|
|
|
|
|
| 321 |
try:
|
| 322 |
+
llm = get_llm(model) # your existing get_llm
|
| 323 |
suggestion_prompt = PromptTemplate(
|
| 324 |
+
input_variables=["prompt"],
|
| 325 |
+
template=prompt
|
| 326 |
)
|
| 327 |
suggestion_chain = LLMChain(llm=llm, prompt=suggestion_prompt)
|
| 328 |
+
# Run the chain without additional inputs, since prompt is fully baked
|
| 329 |
+
raw_text = await asyncio.to_thread(suggestion_chain.run, {})
|
| 330 |
+
# Strip out any leading numbers, bullets or whitespace
|
|
|
|
|
|
|
| 331 |
suggestion_array = [
|
| 332 |
+
re.sub(r'^\s*[\-\d\.\)\s]+', '', line).strip()
|
| 333 |
for line in raw_text.split("\n") if line.strip()
|
| 334 |
]
|
| 335 |
if not suggestion_array:
|
| 336 |
suggestion_array = ["No suggestions available"]
|
| 337 |
return {"suggestions": suggestion_array}
|
| 338 |
except Exception as e:
|
| 339 |
+
# preserve your original error wrapping
|
| 340 |
raise HTTPException(status_code=500, detail=f"Error generating suggestions: {str(e)}")
|
| 341 |
|
| 342 |
@app.post("/generate")
|