Spaces:
Sleeping
Sleeping
Commit ·
0cb15e4
1
Parent(s): a33d10e
Implement custom robust fallback using RunnableLambda in chat_agent.py
Browse files- app/services/chat_agent.py +28 -3
app/services/chat_agent.py
CHANGED
|
@@ -63,6 +63,28 @@ def fetch_product_metrics(product_id: str, seller_id: str) -> str:
|
|
| 63 |
return f"Could not fetch product metrics due to API error: {str(e)}."
|
| 64 |
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
class FallbackChatGroq:
|
| 67 |
def __init__(self, primary: ChatGroq, fallback: ChatGroq):
|
| 68 |
self.primary = primary
|
|
@@ -71,15 +93,18 @@ class FallbackChatGroq:
|
|
| 71 |
def bind_tools(self, tools, **kwargs):
|
| 72 |
bound_primary = self.primary.bind_tools(tools, **kwargs)
|
| 73 |
bound_fallback = self.fallback.bind_tools(tools, **kwargs)
|
| 74 |
-
return
|
| 75 |
|
| 76 |
def with_structured_output(self, schema, **kwargs):
|
| 77 |
struct_primary = self.primary.with_structured_output(schema, **kwargs)
|
| 78 |
struct_fallback = self.fallback.with_structured_output(schema, **kwargs)
|
| 79 |
-
return
|
| 80 |
|
| 81 |
def invoke(self, messages, **kwargs):
|
| 82 |
-
return self.primary
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
def get_chat_agent():
|
|
|
|
| 63 |
return f"Could not fetch product metrics due to API error: {str(e)}."
|
| 64 |
|
| 65 |
|
| 66 |
+
from langchain_core.runnables import RunnableLambda
|
| 67 |
+
|
| 68 |
+
def make_fallback_runnable(primary, fallback):
|
| 69 |
+
def invoke_fn(input, config=None, **kwargs):
|
| 70 |
+
try:
|
| 71 |
+
return primary.invoke(input, config, **kwargs)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
import logging
|
| 74 |
+
logging.getLogger("fallback").warning(f"Primary model failed: {e}. Falling back...")
|
| 75 |
+
return fallback.invoke(input, config, **kwargs)
|
| 76 |
+
|
| 77 |
+
async def ainvoke_fn(input, config=None, **kwargs):
|
| 78 |
+
try:
|
| 79 |
+
return await primary.ainvoke(input, config, **kwargs)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
import logging
|
| 82 |
+
logging.getLogger("fallback").warning(f"Primary model failed: {e}. Falling back...")
|
| 83 |
+
return await fallback.ainvoke(input, config, **kwargs)
|
| 84 |
+
|
| 85 |
+
return RunnableLambda(invoke_fn, afunc=ainvoke_fn)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
class FallbackChatGroq:
|
| 89 |
def __init__(self, primary: ChatGroq, fallback: ChatGroq):
|
| 90 |
self.primary = primary
|
|
|
|
| 93 |
def bind_tools(self, tools, **kwargs):
|
| 94 |
bound_primary = self.primary.bind_tools(tools, **kwargs)
|
| 95 |
bound_fallback = self.fallback.bind_tools(tools, **kwargs)
|
| 96 |
+
return make_fallback_runnable(bound_primary, bound_fallback)
|
| 97 |
|
| 98 |
def with_structured_output(self, schema, **kwargs):
|
| 99 |
struct_primary = self.primary.with_structured_output(schema, **kwargs)
|
| 100 |
struct_fallback = self.fallback.with_structured_output(schema, **kwargs)
|
| 101 |
+
return make_fallback_runnable(struct_primary, struct_fallback)
|
| 102 |
|
| 103 |
def invoke(self, messages, **kwargs):
|
| 104 |
+
return make_fallback_runnable(self.primary, self.fallback).invoke(messages, **kwargs)
|
| 105 |
+
|
| 106 |
+
def ainvoke(self, messages, **kwargs):
|
| 107 |
+
return make_fallback_runnable(self.primary, self.fallback).ainvoke(messages, **kwargs)
|
| 108 |
|
| 109 |
|
| 110 |
def get_chat_agent():
|