Spaces:
Sleeping
Sleeping
Update functions.py
Browse files- functions.py +47 -3
functions.py
CHANGED
|
@@ -1,6 +1,50 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing_extensions import TypedDict, List
|
| 3 |
+
from IPython.display import Image, display
|
| 4 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
| 5 |
+
from langchain.schema import Document
|
| 6 |
+
from langgraph.graph import START, END, StateGraph
|
| 7 |
+
from langchain.prompts import PromptTemplate
|
| 8 |
+
import uuid
|
| 9 |
+
from langchain_exa import ExaSearchRetriever, TextContentsOptions
|
| 10 |
+
from langchain_groq import ChatGroq
|
| 11 |
+
from langchain_community.utilities import GoogleSerperAPIWrapper
|
| 12 |
+
from langchain_chroma import Chroma
|
| 13 |
+
from langchain_community.document_loaders import NewsURLLoader
|
| 14 |
+
from langchain_community.retrievers.wikipedia import WikipediaRetriever
|
| 15 |
+
from sentence_transformers import SentenceTransformer
|
| 16 |
+
from langchain.vectorstores import Chroma
|
| 17 |
+
from langchain_community.document_loaders import UnstructuredURLLoader, NewsURLLoader
|
| 18 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 19 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 20 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 21 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 22 |
+
from langchain_core.output_parsers import JsonOutputParser
|
| 23 |
+
from langchain_community.vectorstores.utils import filter_complex_metadata
|
| 24 |
+
from langchain.schema import Document
|
| 25 |
+
from langgraph.graph import START, END, StateGraph
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
async def predict_custom_agent_answer(example: dict):
|
| 30 |
+
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Invoke the custom graph with the input question
|
| 34 |
+
state_dict = await custom_graph.ainvoke(
|
| 35 |
+
{"question": example["input"], "steps": []}, config
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Check if 'generation' exists and is not empty
|
| 39 |
+
if 'generation' in state_dict and state_dict['generation']:
|
| 40 |
+
return {"response": state_dict["generation"], "steps": state_dict["steps"]}
|
| 41 |
+
else:
|
| 42 |
+
# Raise an exception if the generation is missing or empty
|
| 43 |
+
print("Your question violates toxicity rules or contains sensitive information.")
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
# Raise the exception with a custom message
|
| 47 |
+
print("An error occurred: Try to change the question.")
|
| 48 |
|
| 49 |
|
| 50 |
|