from langchain.agents import tool from langchain_openai import OpenAIEmbeddings from langchain_community.vectorstores.faiss import FAISS from langchain.chains import RetrievalQA from langchain_openai import OpenAI, ChatOpenAI from langchain_core.pydantic_v1 import BaseModel, Field @tool def frequently_asked_questions(input: str): """ Please always use this tool if the user has questions about our offer """ # Load from local storage embeddings = OpenAIEmbeddings() persisted_vectorstore = FAISS.load_local("_rise_faq_db", embeddings) # Use RetrievalQA chain for orchestration qa = RetrievalQA.from_chain_type( llm=ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0), chain_type="stuff", return_source_documents=False, retriever=persisted_vectorstore.as_retriever(search_type="similarity_score_threshold",search_kwargs={"k":3, "score_threshold":0.5})) result = qa.invoke(input) return result @tool def check_eligibility(input: str): """ Use this to check whether a student is eligible to earn classificatory credits """ from flask import request from langchain_community.document_loaders import WebBaseLoader document = WebBaseLoader("https://rise.mmu.ac.uk/wp-content/themes/rise/helpers/user/student_eligibility/chatbotquery.php?query=eligibility&wpid="+request.values.get("user_id")).load() return document[0].page_content class RecommendActivityInput(BaseModel): profile: str = Field(description="should be a penportrait of the user describing their interests and objectives. If they have a specific thing they are interested in, it should state that") @tool("recommend_activity", args_schema=RecommendActivityInput, return_direct=False) def recommend_activity(profile: str) -> str: """ Use this to search the Rise portfolio for relevant activities """ # Load from local storage embeddings = OpenAIEmbeddings() persisted_vectorstore = FAISS.load_local("_rise_product_db", embeddings) # Set Up LLM from agent.prompt import prompt llm = OpenAI(model="gpt-3.5-turbo-instruct", temperature=0) # Use RetrievalQA chain for orchestration qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=persisted_vectorstore.as_retriever(),chain_type_kwargs={"prompt": "speak like a pirate"}) result = qa.invoke("recommend an activity relevant to the following profile: "+profile) return result tools = [frequently_asked_questions, check_eligibility] from langgraph.prebuilt import ToolExecutor tool_executor = ToolExecutor(tools) from langchain_core.utils.function_calling import convert_to_openai_function converted_tools = [convert_to_openai_function(t) for t in tools]