Spaces:
Runtime error
Runtime error
Commit ·
a6fb11a
1
Parent(s): 1120c28
Fixed OpenAI Calls
Browse filesWith the RunnableSequence we have to import AIMessage then also only after it is used we can strip the message.
- app/main.py +19 -2
- requirements.txt +1 -2
app/main.py
CHANGED
|
@@ -19,6 +19,7 @@ from pydantic import BaseModel
|
|
| 19 |
from langchain_openai import ChatOpenAI
|
| 20 |
from langchain.prompts import PromptTemplate
|
| 21 |
from langchain.schema.runnable import RunnableSequence
|
|
|
|
| 22 |
|
| 23 |
# Import the base LLM class to build our custom wrapper
|
| 24 |
from langchain.llms.base import LLM
|
|
@@ -307,11 +308,27 @@ async def get_suggestions(
|
|
| 307 |
)
|
| 308 |
suggestion_chain: RunnableSequence = suggestion_prompt | llm
|
| 309 |
# Run the chain without additional inputs, since prompt is fully baked
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
# Strip out any leading numbers, bullets or whitespace
|
|
|
|
| 312 |
suggestion_array = [
|
| 313 |
re.sub(r'^\s*[\-\d\.\)\s]+', '', line).strip()
|
| 314 |
-
for line in raw_text.split("\n")
|
|
|
|
| 315 |
]
|
| 316 |
if not suggestion_array:
|
| 317 |
suggestion_array = ["No suggestions available"]
|
|
|
|
| 19 |
from langchain_openai import ChatOpenAI
|
| 20 |
from langchain.prompts import PromptTemplate
|
| 21 |
from langchain.schema.runnable import RunnableSequence
|
| 22 |
+
from langchain.schema import AIMessage
|
| 23 |
|
| 24 |
# Import the base LLM class to build our custom wrapper
|
| 25 |
from langchain.llms.base import LLM
|
|
|
|
| 308 |
)
|
| 309 |
suggestion_chain: RunnableSequence = suggestion_prompt | llm
|
| 310 |
# Run the chain without additional inputs, since prompt is fully baked
|
| 311 |
+
raw_result = await asyncio.to_thread(suggestion_chain.invoke, {})
|
| 312 |
+
|
| 313 |
+
# 2) Normalize AIMessage or list[AIMessage] → plain string
|
| 314 |
+
if isinstance(raw_result, AIMessage):
|
| 315 |
+
raw_text = raw_result.content
|
| 316 |
+
elif (
|
| 317 |
+
isinstance(raw_result, (list, tuple))
|
| 318 |
+
and raw_result
|
| 319 |
+
and isinstance(raw_result[0], AIMessage)
|
| 320 |
+
):
|
| 321 |
+
# If multiple AIMessage objects, join their contents
|
| 322 |
+
raw_text = "\n".join(msg.content for msg in raw_result)
|
| 323 |
+
else:
|
| 324 |
+
# Fallback to whatever __str__ gives you
|
| 325 |
+
raw_text = str(raw_result)
|
| 326 |
# Strip out any leading numbers, bullets or whitespace
|
| 327 |
+
# 3) Now it’s safe to split on newlines and clean up bullets
|
| 328 |
suggestion_array = [
|
| 329 |
re.sub(r'^\s*[\-\d\.\)\s]+', '', line).strip()
|
| 330 |
+
for line in raw_text.split("\n")
|
| 331 |
+
if line.strip()
|
| 332 |
]
|
| 333 |
if not suggestion_array:
|
| 334 |
suggestion_array = ["No suggestions available"]
|
requirements.txt
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
httpx
|
| 4 |
-
langchain
|
| 5 |
langgraph
|
| 6 |
openai
|
| 7 |
pydantic
|
| 8 |
loguru
|
| 9 |
python-dotenv
|
| 10 |
huggingface_hub
|
| 11 |
-
langchain-community>=0.2.0
|
| 12 |
langchain-openai
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
httpx
|
| 4 |
+
langchain
|
| 5 |
langgraph
|
| 6 |
openai
|
| 7 |
pydantic
|
| 8 |
loguru
|
| 9 |
python-dotenv
|
| 10 |
huggingface_hub
|
|
|
|
| 11 |
langchain-openai
|