File size: 1,378 Bytes
891fbea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain import LLMChain
from langchain.prompts.prompt import PromptTemplate
from langchain.chains.api.prompt import API_URL_PROMPT_TEMPLATE


def find_value(dictionary: dict, key):
    """

    Recursive function to return the value of key in a dictionary

    (Code was also created by an LLM)

    """
    if key in dictionary:
        return dictionary[key]
    for k, v in dictionary.items():
        if isinstance(v, dict):
            item = find_value(v, key)
            if item is not None:
                return item


def create_custom_response_template(custom_resp: str):
    # Customized Prompt
    API_RESPONSE_PROMPT_TEMPLATE = (
        API_URL_PROMPT_TEMPLATE
        + """ {api_url}



    Here is the response from the API:



    {api_response}""" + f"\n\n{custom_resp}"
    )

    return PromptTemplate(
        input_variables=["api_docs", "question", "api_url", "api_response"],
        template=API_RESPONSE_PROMPT_TEMPLATE,
    )


def custom_api_prompt(llm, chain, question, custom_resp):
    """

    Helper function for separating the api-link creation and the post-processing on the response

    """
    resp_prompt = create_custom_response_template(custom_resp)
    get_answer_chain = LLMChain(llm=llm, prompt=resp_prompt)
    chain.api_answer_chain = get_answer_chain
    return chain.run(question)