Commit 
							
							·
						
						93e630a
	
1
								Parent(s):
							
							873efbe
								
Upload 6 files
Browse files
    	
        utils/__pycache__/ask_human.cpython-310.pyc
    ADDED
    
    | Binary file (1.18 kB). View file | 
|  | 
    	
        utils/__pycache__/model_params.cpython-310.pyc
    ADDED
    
    | Binary file (1.01 kB). View file | 
|  | 
    	
        utils/__pycache__/prompts.cpython-310.pyc
    ADDED
    
    | Binary file (1.42 kB). View file | 
|  | 
    	
        utils/ask_human.py
    ADDED
    
    | @@ -0,0 +1,32 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            """ 
         | 
| 2 | 
            +
            Custom Langchain tool to ask human
         | 
| 3 | 
            +
            """
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            import time
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            import streamlit as st
         | 
| 8 | 
            +
            from langchain.tools.base import BaseTool
         | 
| 9 | 
            +
             | 
| 10 | 
            +
             | 
| 11 | 
            +
            class CustomAskHumanTool(BaseTool):
         | 
| 12 | 
            +
                """Tool that asks user for input."""
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                name = "AskHuman"
         | 
| 15 | 
            +
                description = """Use this tool if you don't find a specific answer using KendraRetrievalTool.\
         | 
| 16 | 
            +
            Ask the human to clarify the question or provide the missing information.\
         | 
| 17 | 
            +
            The input should be a question for the human."""
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def _run(
         | 
| 20 | 
            +
                    self,
         | 
| 21 | 
            +
                    query: str,
         | 
| 22 | 
            +
                    run_manager=None,
         | 
| 23 | 
            +
                ) -> str:
         | 
| 24 | 
            +
                    if "user_answer" not in st.session_state:
         | 
| 25 | 
            +
                        answer_container = st.chat_message("assistant", avatar="🦜")
         | 
| 26 | 
            +
                        answer_container.write(query)
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                        answer = st.text_input("Enter your answer", key="user_answer")
         | 
| 29 | 
            +
                        while answer == "":
         | 
| 30 | 
            +
                            time.sleep(1)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                    return st.session_state["user_answer"]
         | 
    	
        utils/model_params.py
    ADDED
    
    | @@ -0,0 +1,51 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            """
         | 
| 2 | 
            +
            Utilities for modeling
         | 
| 3 | 
            +
            """
         | 
| 4 | 
            +
             | 
| 5 | 
            +
             | 
| 6 | 
            +
            def get_model_params(
         | 
| 7 | 
            +
                model_id: str,
         | 
| 8 | 
            +
                params: dict,
         | 
| 9 | 
            +
            ) -> dict:
         | 
| 10 | 
            +
                """
         | 
| 11 | 
            +
                Set up a dictionary with model parameters named appropriately for Bedrock
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                Parameters
         | 
| 14 | 
            +
                ----------
         | 
| 15 | 
            +
                model_id : str
         | 
| 16 | 
            +
                    Model name
         | 
| 17 | 
            +
                params : dict
         | 
| 18 | 
            +
                    Inference parameters
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                Returns
         | 
| 21 | 
            +
                -------
         | 
| 22 | 
            +
                dict
         | 
| 23 | 
            +
                    _description_
         | 
| 24 | 
            +
                """
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                model_params = {}
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                # name parameters based on the model id
         | 
| 29 | 
            +
                if model_id.startswith("amazon"):
         | 
| 30 | 
            +
                    model_params = {
         | 
| 31 | 
            +
                        "maxTokenCount": params["answer_length"],
         | 
| 32 | 
            +
                        "stopSequences": params["stop_words"],
         | 
| 33 | 
            +
                        "temperature": params["temperature"],
         | 
| 34 | 
            +
                        "topP": params["top_p"],
         | 
| 35 | 
            +
                    }
         | 
| 36 | 
            +
                elif model_id.startswith("anthropic"):
         | 
| 37 | 
            +
                    model_params = {
         | 
| 38 | 
            +
                        "max_tokens_to_sample": params["answer_length"],
         | 
| 39 | 
            +
                        "stop_sequences": params["stop_words"],
         | 
| 40 | 
            +
                        "temperature": params["temperature"],
         | 
| 41 | 
            +
                        "top_p": params["top_p"],
         | 
| 42 | 
            +
                    }
         | 
| 43 | 
            +
                elif model_id.startswith("ai21"):
         | 
| 44 | 
            +
                    model_params = {
         | 
| 45 | 
            +
                        "maxTokens": params["answer_length"],
         | 
| 46 | 
            +
                        "stopSequences": params["stop_words"],
         | 
| 47 | 
            +
                        "temperature": params["temperature"],
         | 
| 48 | 
            +
                        "topP": params["top_p"],
         | 
| 49 | 
            +
                    }
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                return model_params
         | 
    	
        utils/prompts.py
    ADDED
    
    | @@ -0,0 +1,49 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            """ 
         | 
| 2 | 
            +
            Custom Langchain prompt templates
         | 
| 3 | 
            +
            """
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            from langchain.prompts import PromptTemplate
         | 
| 6 | 
            +
             | 
| 7 | 
            +
             | 
| 8 | 
            +
            def create_qa_prompt() -> PromptTemplate:
         | 
| 9 | 
            +
                """
         | 
| 10 | 
            +
                Prompt for retrieval QA chain
         | 
| 11 | 
            +
                """
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                template = """\n\nHuman: Use the following pieces of context to answer the question at the end.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            {context}
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            Question: {question}
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            \n\nAssistant:
         | 
| 20 | 
            +
            Answer:"""
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                return PromptTemplate(template=template, input_variables=["context", "question"])
         | 
| 23 | 
            +
             | 
| 24 | 
            +
             | 
| 25 | 
            +
            def create_agent_prompt() -> PromptTemplate:
         | 
| 26 | 
            +
                """
         | 
| 27 | 
            +
                Prompt for the agent
         | 
| 28 | 
            +
                """
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                prefix = """\n\nHuman: Answer the following questions as best you can. You have access to the following tools:"""
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                format_instructions = """Use the following format:
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            Question: the input question you must answer
         | 
| 35 | 
            +
            Thought: you should always think about what to do
         | 
| 36 | 
            +
            Action: the action to take, should be one of [{tool_names}]
         | 
| 37 | 
            +
            Action Input: the input to the action
         | 
| 38 | 
            +
            Observation: the result of the action
         | 
| 39 | 
            +
            ... (this Thought/Action/Action Input/Observation can repeat N times)
         | 
| 40 | 
            +
            Thought: I now know the final answer
         | 
| 41 | 
            +
            Final Answer: the final answer to the original input question"""
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                suffix = """Begin!
         | 
| 44 | 
            +
            Question: {input}
         | 
| 45 | 
            +
            \n\nAssistant:
         | 
| 46 | 
            +
            Thought: {agent_scratchpad}
         | 
| 47 | 
            +
            """
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                return prefix, format_instructions, suffix
         | 
 
			
