File size: 2,780 Bytes
76c5345
 
 
 
46fc259
796ceef
76c5345
 
a0503bc
796ceef
 
46fc259
796ceef
76c5345
 
 
 
 
 
46fc259
 
 
 
 
76c5345
 
 
33b89d4
 
 
 
 
 
 
 
 
 
 
796ceef
 
76c5345
796ceef
 
 
 
 
 
 
 
 
76c5345
 
 
796ceef
 
 
 
76c5345
796ceef
 
76c5345
 
33b89d4
796ceef
 
 
 
 
 
76c5345
796ceef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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]