├── README.md ├── data ├── PDFs │ ├── How_to_build_your_carreer_in_AI.pdf │ └── What Should Data Science Education Do with Large Language Models?.pdf └── langchain_doc_small │ ├── 9_Generic_Functionality_Generic.txt │ ├── 7_LLMs_LLMs_Note.txt │ ├── 18_Integrations_Integrations_The.txt │ ├── 27_Bedrock_Contents_Using.txt │ ├── 5_Models_Contents_Model.txt │ ├── 30_C_Transformers_C.txt │ ├── 19_AI21_AI21_AI21.txt │ ├── 20_Aleph_Alpha_Aleph.txt │ ├── 24_Banana_Banana_Banana.txt │ ├── 12_How_and_why_.txt │ ├── 12_How_(and_why).txt │ ├── 22_Aviary_Aviary_Aviary.txt │ ├── 15_How_to_serialize.txt │ ├── 11_How_to_write.txt │ ├── 21_Anyscale_Anyscale_Anyscale.txt │ ├── 29_Cohere_Cohere_Cohere.txt │ ├── 33_ForefrontAI_Contents_Imports.txt │ ├── 35_GooseAI_Contents_Install.txt │ ├── 26_Beam_Beam_Beam.txt │ ├── 28_CerebriumAI_Contents_Install.txt │ ├── 6_Getting_Started_Contents.txt │ ├── 34_Google_Cloud_Platform.txt │ ├── 32_DeepInfra_Contents_Imports.txt │ ├── 10_How_to_use.txt │ ├── 38_Hugging_Face_Pipeline.txt │ ├── 3_Concepts_Contents_Chain.txt │ ├── 23_Azure_OpenAI_Contents.txt │ ├── 25_Baseten_Contents_Baseten.txt │ ├── 17_How_to_track.txt │ ├── 36_GPT4All_Contents_Specify.txt │ ├── 8_Getting_Started_Getting.txt │ ├── 37_Hugging_Face_Hub.txt │ ├── 16_How_to_stream.txt │ ├── 4_Tutorials_Contents_DeepLearning_AI.txt │ ├── 0_Welcome_to_LangChain.txt │ ├── 1_Welcome_to_LangChain.txt │ ├── 31_Databricks_Contents_Wrapping.txt │ ├── 13_How_and_why_.txt │ ├── 13_How_(and_why).txt │ ├── 14_How_to_cache.txt │ └── 2_Quickstart_Guide_Contents.txt └── ChromaDB.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Mistral7B 2 | This is a repository where I show how to use Mistral 7B 3 | -------------------------------------------------------------------------------- /data/PDFs/How_to_build_your_carreer_in_AI.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubentak/Mistral7B/main/data/PDFs/How_to_build_your_carreer_in_AI.pdf -------------------------------------------------------------------------------- /data/PDFs/What Should Data Science Education Do with Large Language Models?.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubentak/Mistral7B/main/data/PDFs/What Should Data Science Education Do with Large Language Models?.pdf -------------------------------------------------------------------------------- /data/langchain_doc_small/9_Generic_Functionality_Generic.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | Generic Functionality 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Generic Functionality# 25 | The examples here all address certain “how-to” guides for working with LLMs. 26 | 27 | 28 | How to use the async API for LLMs 29 | How to write a custom LLM wrapper 30 | How (and why) to use the fake LLM 31 | How (and why) to use the human input LLM 32 | How to cache LLM calls 33 | How to serialize LLM classes 34 | How to stream LLM and Chat Model responses 35 | How to track token usage 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | previous 47 | Getting Started 48 | 49 | 50 | 51 | 52 | next 53 | How to use the async API for LLMs 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | By Harrison Chase 67 | 68 | 69 | 70 | 71 | 72 | © Copyright 2023, Harrison Chase. 73 | 74 | 75 | 76 | 77 | 78 | Last updated on Jun 14, 2023. 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /data/langchain_doc_small/7_LLMs_LLMs_Note.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | LLMs 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | LLMs# 25 | 26 | Note 27 | Conceptual Guide 28 | 29 | Large Language Models (LLMs) are a core component of LangChain. 30 | LangChain is not a provider of LLMs, but rather provides a standard interface through which 31 | you can interact with a variety of LLMs. 32 | The following sections of documentation are provided: 33 | 34 | Getting Started: An overview of all the functionality the LangChain LLM class provides. 35 | How-To Guides: A collection of how-to guides. These highlight how to accomplish various objectives with our LLM class (streaming, async, etc). 36 | Integrations: A collection of examples on how to integrate different LLM providers with LangChain (OpenAI, Hugging Face, etc). 37 | Reference: API reference documentation for all LLM classes. 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | previous 50 | Getting Started 51 | 52 | 53 | 54 | 55 | next 56 | Getting Started 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | By Harrison Chase 70 | 71 | 72 | 73 | 74 | 75 | © Copyright 2023, Harrison Chase. 76 | 77 | 78 | 79 | 80 | 81 | Last updated on Jun 14, 2023. 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /data/langchain_doc_small/18_Integrations_Integrations_The.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Integrations 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Integrations# 28 | The examples here are all “how-to” guides for how to integrate with various LLM providers. 29 | 30 | 31 | AI21 32 | Aleph Alpha 33 | Anyscale 34 | Aviary 35 | Azure OpenAI 36 | Banana 37 | Baseten 38 | Setup 39 | Single model call 40 | Chained model calls 41 | Beam 42 | Bedrock 43 | CerebriumAI 44 | Cohere 45 | C Transformers 46 | Databricks 47 | DeepInfra 48 | ForefrontAI 49 | Google Cloud Platform Vertex AI PaLM 50 | GooseAI 51 | GPT4All 52 | Hugging Face Hub 53 | Hugging Face Pipeline 54 | Huggingface TextGen Inference 55 | Jsonformer 56 | Llama-cpp 57 | Manifest 58 | Modal 59 | MosaicML 60 | NLP Cloud 61 | OpenAI 62 | OpenLM 63 | Petals 64 | PipelineAI 65 | Prediction Guard 66 | Control the output structure/ type of LLMs 67 | Chaining 68 | PromptLayer OpenAI 69 | ReLLM 70 | Replicate 71 | Runhouse 72 | SageMaker Endpoint 73 | StochasticAI 74 | Writer 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | previous 86 | How to track token usage 87 | 88 | 89 | 90 | 91 | next 92 | AI21 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | By Harrison Chase 106 | 107 | 108 | 109 | 110 | 111 | © Copyright 2023, Harrison Chase. 112 | 113 | 114 | 115 | 116 | 117 | Last updated on Jun 13, 2023. 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /data/langchain_doc_small/27_Bedrock_Contents_Using.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Bedrock 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Using in a conversation chain 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | Bedrock# 38 | Amazon Bedrock is a fully managed service that makes FMs from leading AI startups and Amazon available via an API, so you can choose from a wide range of FMs to find the model that is best suited for your use case 39 | 40 | 41 | %pip install boto3 42 | 43 | 44 | 45 | 46 | 47 | 48 | from langchain.llms.bedrock import Bedrock 49 | 50 | llm = Bedrock(credentials_profile_name="bedrock-admin", model_id="amazon.titan-tg1-large") 51 | 52 | 53 | 54 | 55 | 56 | Using in a conversation chain# 57 | 58 | 59 | from langchain.chains import ConversationChain 60 | from langchain.memory import ConversationBufferMemory 61 | 62 | conversation = ConversationChain( 63 | llm=llm, 64 | verbose=True, 65 | memory=ConversationBufferMemory() 66 | ) 67 | 68 | conversation.predict(input="Hi there!") 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | previous 83 | Beam 84 | 85 | 86 | 87 | 88 | next 89 | CerebriumAI 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | Contents 100 | 101 | 102 | 103 | Using in a conversation chain 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | By Harrison Chase 114 | 115 | 116 | 117 | 118 | 119 | © Copyright 2023, Harrison Chase. 120 | 121 | 122 | 123 | 124 | 125 | Last updated on Jun 13, 2023. 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /data/langchain_doc_small/5_Models_Contents_Model.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Models 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | Model Types 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | Models# 36 | 37 | Note 38 | Conceptual Guide 39 | 40 | This section of the documentation deals with different types of models that are used in LangChain. 41 | On this page we will go over the model types at a high level, 42 | but we have individual pages for each model type. 43 | The pages contain more detailed “how-to” guides for working with that model, 44 | as well as a list of different model providers. 45 | 46 | 47 | 48 | 49 | Getting Started: An overview of the models. 50 | 51 | 52 | Model Types# 53 | 54 | LLMs: Large Language Models (LLMs) take a text string as input and return a text string as output. 55 | Chat Models: Chat Models are usually backed by a language model, but their APIs are more structured. 56 | Specifically, these models take a list of Chat Messages as input, and return a Chat Message. 57 | Text Embedding Models: Text embedding models take text as input and return a list of floats. 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | previous 71 | Tutorials 72 | 73 | 74 | 75 | 76 | next 77 | Getting Started 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | Contents 88 | 89 | 90 | 91 | Model Types 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | By Harrison Chase 102 | 103 | 104 | 105 | 106 | 107 | © Copyright 2023, Harrison Chase. 108 | 109 | 110 | 111 | 112 | 113 | Last updated on Jun 14, 2023. 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /data/langchain_doc_small/30_C_Transformers_C.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | C Transformers 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | C Transformers# 28 | The C Transformers library provides Python bindings for GGML models. 29 | This example goes over how to use LangChain to interact with C Transformers models. 30 | Install 31 | 32 | 33 | %pip install ctransformers 34 | 35 | 36 | 37 | 38 | Load Model 39 | 40 | 41 | from langchain.llms import CTransformers 42 | 43 | llm = CTransformers(model='marella/gpt-2-ggml') 44 | 45 | 46 | 47 | 48 | Generate Text 49 | 50 | 51 | print(llm('AI is going to')) 52 | 53 | 54 | 55 | 56 | Streaming 57 | 58 | 59 | from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler 60 | 61 | llm = CTransformers(model='marella/gpt-2-ggml', callbacks=[StreamingStdOutCallbackHandler()]) 62 | 63 | response = llm('AI is going to') 64 | 65 | 66 | 67 | 68 | LLMChain 69 | 70 | 71 | from langchain import PromptTemplate, LLMChain 72 | 73 | template = """Question: {question} 74 | 75 | Answer:""" 76 | 77 | prompt = PromptTemplate(template=template, input_variables=['question']) 78 | 79 | llm_chain = LLMChain(prompt=prompt, llm=llm) 80 | 81 | response = llm_chain.run('What is AI?') 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | previous 95 | Cohere 96 | 97 | 98 | 99 | 100 | next 101 | Databricks 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | By Harrison Chase 115 | 116 | 117 | 118 | 119 | 120 | © Copyright 2023, Harrison Chase. 121 | 122 | 123 | 124 | 125 | 126 | Last updated on Jun 13, 2023. 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /data/langchain_doc_small/19_AI21_AI21_AI21.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | AI21 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | AI21# 27 | AI21 Studio provides API access to Jurassic-2 large language models. 28 | This example goes over how to use LangChain to interact with AI21 models. 29 | 30 | 31 | # install the package: 32 | !pip install ai21 33 | 34 | 35 | 36 | 37 | 38 | 39 | # get AI21_API_KEY. Use https://studio.ai21.com/account/account 40 | 41 | from getpass import getpass 42 | AI21_API_KEY = getpass() 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | from langchain.llms import AI21 52 | from langchain import PromptTemplate, LLMChain 53 | 54 | 55 | 56 | 57 | 58 | 59 | template = """Question: {question} 60 | 61 | Answer: Let's think step by step.""" 62 | 63 | prompt = PromptTemplate(template=template, input_variables=["question"]) 64 | 65 | 66 | 67 | 68 | 69 | 70 | llm = AI21(ai21_api_key=AI21_API_KEY) 71 | 72 | 73 | 74 | 75 | 76 | 77 | llm_chain = LLMChain(prompt=prompt, llm=llm) 78 | 79 | 80 | 81 | 82 | 83 | 84 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 85 | 86 | llm_chain.run(question) 87 | 88 | 89 | 90 | 91 | '\n1. What year was Justin Bieber born?\nJustin Bieber was born in 1994.\n2. What team won the Super Bowl in 1994?\nThe Dallas Cowboys won the Super Bowl in 1994.' 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | previous 105 | Integrations 106 | 107 | 108 | 109 | 110 | next 111 | Aleph Alpha 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | By Harrison Chase 125 | 126 | 127 | 128 | 129 | 130 | © Copyright 2023, Harrison Chase. 131 | 132 | 133 | 134 | 135 | 136 | Last updated on Jun 13, 2023. 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /data/langchain_doc_small/20_Aleph_Alpha_Aleph.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Aleph Alpha 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Aleph Alpha# 27 | The Luminous series is a family of large language models. 28 | This example goes over how to use LangChain to interact with Aleph Alpha models 29 | 30 | 31 | # Install the package 32 | !pip install aleph-alpha-client 33 | 34 | 35 | 36 | 37 | 38 | 39 | # create a new token: https://docs.aleph-alpha.com/docs/account/#create-a-new-token 40 | 41 | from getpass import getpass 42 | 43 | ALEPH_ALPHA_API_KEY = getpass() 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | from langchain.llms import AlephAlpha 53 | from langchain import PromptTemplate, LLMChain 54 | 55 | 56 | 57 | 58 | 59 | 60 | template = """Q: {question} 61 | 62 | A:""" 63 | 64 | prompt = PromptTemplate(template=template, input_variables=["question"]) 65 | 66 | 67 | 68 | 69 | 70 | 71 | llm = AlephAlpha(model="luminous-extended", maximum_tokens=20, stop_sequences=["Q:"], aleph_alpha_api_key=ALEPH_ALPHA_API_KEY) 72 | 73 | 74 | 75 | 76 | 77 | 78 | llm_chain = LLMChain(prompt=prompt, llm=llm) 79 | 80 | 81 | 82 | 83 | 84 | 85 | question = "What is AI?" 86 | 87 | llm_chain.run(question) 88 | 89 | 90 | 91 | 92 | ' Artificial Intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems.\n' 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | previous 106 | AI21 107 | 108 | 109 | 110 | 111 | next 112 | Anyscale 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | By Harrison Chase 126 | 127 | 128 | 129 | 130 | 131 | © Copyright 2023, Harrison Chase. 132 | 133 | 134 | 135 | 136 | 137 | Last updated on Jun 13, 2023. 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /data/langchain_doc_small/24_Banana_Banana_Banana.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Banana 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Banana# 28 | Banana is focused on building the machine learning infrastructure. 29 | This example goes over how to use LangChain to interact with Banana models 30 | 31 | 32 | # Install the package https://docs.banana.dev/banana-docs/core-concepts/sdks/python 33 | !pip install banana-dev 34 | 35 | 36 | 37 | 38 | 39 | 40 | # get new tokens: https://app.banana.dev/ 41 | # We need two tokens, not just an `api_key`: `BANANA_API_KEY` and `YOUR_MODEL_KEY` 42 | 43 | import os 44 | from getpass import getpass 45 | 46 | os.environ["BANANA_API_KEY"] = "YOUR_API_KEY" 47 | # OR 48 | # BANANA_API_KEY = getpass() 49 | 50 | 51 | 52 | 53 | 54 | 55 | from langchain.llms import Banana 56 | from langchain import PromptTemplate, LLMChain 57 | 58 | 59 | 60 | 61 | 62 | 63 | template = """Question: {question} 64 | 65 | Answer: Let's think step by step.""" 66 | 67 | prompt = PromptTemplate(template=template, input_variables=["question"]) 68 | 69 | 70 | 71 | 72 | 73 | 74 | llm = Banana(model_key="YOUR_MODEL_KEY") 75 | 76 | 77 | 78 | 79 | 80 | 81 | llm_chain = LLMChain(prompt=prompt, llm=llm) 82 | 83 | 84 | 85 | 86 | 87 | 88 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 89 | 90 | llm_chain.run(question) 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | previous 104 | Azure OpenAI 105 | 106 | 107 | 108 | 109 | next 110 | Baseten 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | By Harrison Chase 124 | 125 | 126 | 127 | 128 | 129 | © Copyright 2023, Harrison Chase. 130 | 131 | 132 | 133 | 134 | 135 | Last updated on Jun 13, 2023. 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /data/langchain_doc_small/12_How_and_why_.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | How (and why) to use the fake LLM 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | How (and why) to use the fake LLM# 25 | We expose a fake LLM class that can be used for testing. This allows you to mock out calls to the LLM and simulate what would happen if the LLM responded in a certain way. 26 | In this notebook we go over how to use this. 27 | We start this with using the FakeLLM in an agent. 28 | 29 | 30 | from langchain.llms.fake import FakeListLLM 31 | 32 | 33 | 34 | 35 | 36 | 37 | from langchain.agents import load_tools 38 | from langchain.agents import initialize_agent 39 | from langchain.agents import AgentType 40 | 41 | 42 | 43 | 44 | 45 | 46 | tools = load_tools(["python_repl"]) 47 | 48 | 49 | 50 | 51 | 52 | 53 | responses=[ 54 | "Action: Python REPL\nAction Input: print(2 + 2)", 55 | "Final Answer: 4" 56 | ] 57 | llm = FakeListLLM(responses=responses) 58 | 59 | 60 | 61 | 62 | 63 | 64 | agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 65 | 66 | 67 | 68 | 69 | 70 | 71 | agent.run("whats 2 + 2") 72 | 73 | 74 | 75 | 76 | > Entering new AgentExecutor chain... 77 | Action: Python REPL 78 | Action Input: print(2 + 2) 79 | Observation: 4 80 | 81 | Thought:Final Answer: 4 82 | 83 | > Finished chain. 84 | 85 | 86 | '4' 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | previous 100 | How to write a custom LLM wrapper 101 | 102 | 103 | 104 | 105 | next 106 | How (and why) to use the human input LLM 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | By Harrison Chase 120 | 121 | 122 | 123 | 124 | 125 | © Copyright 2023, Harrison Chase. 126 | 127 | 128 | 129 | 130 | 131 | Last updated on Jun 14, 2023. 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /data/langchain_doc_small/12_How_(and_why).txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | How (and why) to use the fake LLM 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | How (and why) to use the fake LLM# 27 | We expose a fake LLM class that can be used for testing. This allows you to mock out calls to the LLM and simulate what would happen if the LLM responded in a certain way. 28 | In this notebook we go over how to use this. 29 | We start this with using the FakeLLM in an agent. 30 | 31 | 32 | from langchain.llms.fake import FakeListLLM 33 | 34 | 35 | 36 | 37 | 38 | 39 | from langchain.agents import load_tools 40 | from langchain.agents import initialize_agent 41 | from langchain.agents import AgentType 42 | 43 | 44 | 45 | 46 | 47 | 48 | tools = load_tools(["python_repl"]) 49 | 50 | 51 | 52 | 53 | 54 | 55 | responses=[ 56 | "Action: Python REPL\nAction Input: print(2 + 2)", 57 | "Final Answer: 4" 58 | ] 59 | llm = FakeListLLM(responses=responses) 60 | 61 | 62 | 63 | 64 | 65 | 66 | agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 67 | 68 | 69 | 70 | 71 | 72 | 73 | agent.run("whats 2 + 2") 74 | 75 | 76 | 77 | 78 | > Entering new AgentExecutor chain... 79 | Action: Python REPL 80 | Action Input: print(2 + 2) 81 | Observation: 4 82 | 83 | Thought:Final Answer: 4 84 | 85 | > Finished chain. 86 | 87 | 88 | '4' 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | previous 102 | How to write a custom LLM wrapper 103 | 104 | 105 | 106 | 107 | next 108 | How (and why) to use the human input LLM 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | By Harrison Chase 122 | 123 | 124 | 125 | 126 | 127 | © Copyright 2023, Harrison Chase. 128 | 129 | 130 | 131 | 132 | 133 | Last updated on Jun 13, 2023. 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /data/langchain_doc_small/22_Aviary_Aviary_Aviary.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Aviary 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Aviary# 28 | Aviary is an open source tooklit for evaluating and deploying production open source LLMs. 29 | This example goes over how to use LangChain to interact with Aviary. You can try Aviary out https://aviary.anyscale.com. 30 | You can find out more about Aviary at https://github.com/ray-project/aviary. 31 | One Aviary instance can serve multiple models. You can get a list of the available models by using the cli: 32 | % aviary models 33 | Or you can connect directly to the endpoint and get a list of available models by using the /models endpoint. 34 | The constructor requires a url for an Aviary backend, and optionally a token to validate the connection. 35 | 36 | 37 | import os 38 | from langchain.llms import Aviary 39 | 40 | 41 | 42 | 43 | 44 | 45 | llm = Aviary(model='amazon/LightGPT', aviary_url=os.environ['AVIARY_URL'], aviary_token=os.environ['AVIARY_TOKEN']) 46 | 47 | 48 | 49 | 50 | 51 | 52 | result = llm.predict('What is the meaning of love?') 53 | print(result) 54 | 55 | 56 | 57 | 58 | Love is an emotion that involves feelings of attraction, affection and empathy for another person. It can also refer to a deep bond between two people or groups of people. Love can be expressed in many different ways, such as through words, actions, gestures, music, art, literature, and other forms of communication. 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | previous 72 | Anyscale 73 | 74 | 75 | 76 | 77 | next 78 | Azure OpenAI 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | By Harrison Chase 92 | 93 | 94 | 95 | 96 | 97 | © Copyright 2023, Harrison Chase. 98 | 99 | 100 | 101 | 102 | 103 | Last updated on Jun 13, 2023. 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /data/langchain_doc_small/15_How_to_serialize.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | How to serialize LLM classes 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | Loading 27 | Saving 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | How to serialize LLM classes# 37 | This notebook walks through how to write and read an LLM Configuration to and from disk. This is useful if you want to save the configuration for a given LLM (e.g., the provider, the temperature, etc). 38 | 39 | 40 | from langchain.llms import OpenAI 41 | from langchain.llms.loading import load_llm 42 | 43 | 44 | 45 | 46 | 47 | Loading# 48 | First, lets go over loading an LLM from disk. LLMs can be saved on disk in two formats: json or yaml. No matter the extension, they are loaded in the same way. 49 | 50 | 51 | !cat llm.json 52 | 53 | 54 | 55 | 56 | { 57 | "model_name": "text-davinci-003", 58 | "temperature": 0.7, 59 | "max_tokens": 256, 60 | "top_p": 1.0, 61 | "frequency_penalty": 0.0, 62 | "presence_penalty": 0.0, 63 | "n": 1, 64 | "best_of": 1, 65 | "request_timeout": null, 66 | "_type": "openai" 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | llm = load_llm("llm.json") 75 | 76 | 77 | 78 | 79 | 80 | 81 | !cat llm.yaml 82 | 83 | 84 | 85 | 86 | _type: openai 87 | best_of: 1 88 | frequency_penalty: 0.0 89 | max_tokens: 256 90 | model_name: text-davinci-003 91 | n: 1 92 | presence_penalty: 0.0 93 | request_timeout: null 94 | temperature: 0.7 95 | top_p: 1.0 96 | 97 | 98 | 99 | 100 | 101 | 102 | llm = load_llm("llm.yaml") 103 | 104 | 105 | 106 | 107 | 108 | 109 | Saving# 110 | If you want to go from an LLM in memory to a serialized version of it, you can do so easily by calling the .save method. Again, this supports both json and yaml. 111 | 112 | 113 | llm.save("llm.json") 114 | 115 | 116 | 117 | 118 | 119 | 120 | llm.save("llm.yaml") 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | previous 135 | How to cache LLM calls 136 | 137 | 138 | 139 | 140 | next 141 | How to stream LLM and Chat Model responses 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | Contents 152 | 153 | 154 | 155 | Loading 156 | Saving 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | By Harrison Chase 167 | 168 | 169 | 170 | 171 | 172 | © Copyright 2023, Harrison Chase. 173 | 174 | 175 | 176 | 177 | 178 | Last updated on Jun 14, 2023. 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /data/langchain_doc_small/11_How_to_write.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | How to write a custom LLM wrapper 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | How to write a custom LLM wrapper# 25 | This notebook goes over how to create a custom LLM wrapper, in case you want to use your own LLM or a different wrapper than one that is supported in LangChain. 26 | There is only one required thing that a custom LLM needs to implement: 27 | 28 | A _call method that takes in a string, some optional stop words, and returns a string 29 | 30 | There is a second optional thing it can implement: 31 | 32 | An _identifying_params property that is used to help with printing of this class. Should return a dictionary. 33 | 34 | Let’s implement a very simple custom LLM that just returns the first N characters of the input. 35 | 36 | 37 | from typing import Any, List, Mapping, Optional 38 | 39 | from langchain.callbacks.manager import CallbackManagerForLLMRun 40 | from langchain.llms.base import LLM 41 | 42 | 43 | 44 | 45 | 46 | 47 | class CustomLLM(LLM): 48 | 49 | n: int 50 | 51 | @property 52 | def _llm_type(self) -> str: 53 | return "custom" 54 | 55 | def _call( 56 | self, 57 | prompt: str, 58 | stop: Optional[List[str]] = None, 59 | run_manager: Optional[CallbackManagerForLLMRun] = None, 60 | ) -> str: 61 | if stop is not None: 62 | raise ValueError("stop kwargs are not permitted.") 63 | return prompt[:self.n] 64 | 65 | @property 66 | def _identifying_params(self) -> Mapping[str, Any]: 67 | """Get the identifying parameters.""" 68 | return {"n": self.n} 69 | 70 | 71 | 72 | 73 | We can now use this as an any other LLM. 74 | 75 | 76 | llm = CustomLLM(n=10) 77 | 78 | 79 | 80 | 81 | 82 | 83 | llm("This is a foobar thing") 84 | 85 | 86 | 87 | 88 | 'This is a ' 89 | 90 | 91 | 92 | 93 | We can also print the LLM and see its custom print. 94 | 95 | 96 | print(llm) 97 | 98 | 99 | 100 | 101 | CustomLLM 102 | Params: {'n': 10} 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | previous 116 | How to use the async API for LLMs 117 | 118 | 119 | 120 | 121 | next 122 | How (and why) to use the fake LLM 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | By Harrison Chase 136 | 137 | 138 | 139 | 140 | 141 | © Copyright 2023, Harrison Chase. 142 | 143 | 144 | 145 | 146 | 147 | Last updated on Jun 14, 2023. 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /data/langchain_doc_small/21_Anyscale_Anyscale_Anyscale.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Anyscale 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Anyscale# 27 | Anyscale is a fully-managed Ray platform, on which you can build, deploy, and manage scalable AI and Python applications 28 | This example goes over how to use LangChain to interact with Anyscale service 29 | 30 | 31 | import os 32 | 33 | os.environ["ANYSCALE_SERVICE_URL"] = ANYSCALE_SERVICE_URL 34 | os.environ["ANYSCALE_SERVICE_ROUTE"] = ANYSCALE_SERVICE_ROUTE 35 | os.environ["ANYSCALE_SERVICE_TOKEN"] = ANYSCALE_SERVICE_TOKEN 36 | 37 | 38 | 39 | 40 | 41 | 42 | from langchain.llms import Anyscale 43 | from langchain import PromptTemplate, LLMChain 44 | 45 | 46 | 47 | 48 | 49 | 50 | template = """Question: {question} 51 | 52 | Answer: Let's think step by step.""" 53 | 54 | prompt = PromptTemplate(template=template, input_variables=["question"]) 55 | 56 | 57 | 58 | 59 | 60 | 61 | llm = Anyscale() 62 | 63 | 64 | 65 | 66 | 67 | 68 | llm_chain = LLMChain(prompt=prompt, llm=llm) 69 | 70 | 71 | 72 | 73 | 74 | 75 | question = "When was George Washington president?" 76 | 77 | llm_chain.run(question) 78 | 79 | 80 | 81 | 82 | With Ray, we can distribute the queries without asyncrhonized implementation. This not only applies to Anyscale LLM model, but to any other Langchain LLM models which do not have _acall or _agenerate implemented 83 | 84 | 85 | prompt_list = [ 86 | "When was George Washington president?", 87 | "Explain to me the difference between nuclear fission and fusion.", 88 | "Give me a list of 5 science fiction books I should read next.", 89 | "Explain the difference between Spark and Ray.", 90 | "Suggest some fun holiday ideas.", 91 | "Tell a joke.", 92 | "What is 2+2?", 93 | "Explain what is machine learning like I am five years old.", 94 | "Explain what is artifical intelligence.", 95 | ] 96 | 97 | 98 | 99 | 100 | 101 | 102 | import ray 103 | 104 | @ray.remote 105 | def send_query(llm, prompt): 106 | resp = llm(prompt) 107 | return resp 108 | 109 | futures = [send_query.remote(llm, prompt) for prompt in prompt_list] 110 | results = ray.get(futures) 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | previous 124 | Aleph Alpha 125 | 126 | 127 | 128 | 129 | next 130 | Aviary 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | By Harrison Chase 144 | 145 | 146 | 147 | 148 | 149 | © Copyright 2023, Harrison Chase. 150 | 151 | 152 | 153 | 154 | 155 | Last updated on Jun 13, 2023. 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | -------------------------------------------------------------------------------- /data/langchain_doc_small/29_Cohere_Cohere_Cohere.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Cohere 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Cohere# 27 | 28 | Cohere is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions. 29 | 30 | This example goes over how to use LangChain to interact with Cohere models. 31 | 32 | 33 | # Install the package 34 | !pip install cohere 35 | 36 | 37 | 38 | 39 | 40 | 41 | # get a new token: https://dashboard.cohere.ai/ 42 | 43 | from getpass import getpass 44 | 45 | COHERE_API_KEY = getpass() 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | from langchain.llms import Cohere 55 | from langchain import PromptTemplate, LLMChain 56 | 57 | 58 | 59 | 60 | 61 | 62 | template = """Question: {question} 63 | 64 | Answer: Let's think step by step.""" 65 | 66 | prompt = PromptTemplate(template=template, input_variables=["question"]) 67 | 68 | 69 | 70 | 71 | 72 | 73 | llm = Cohere(cohere_api_key=COHERE_API_KEY) 74 | 75 | 76 | 77 | 78 | 79 | 80 | llm_chain = LLMChain(prompt=prompt, llm=llm) 81 | 82 | 83 | 84 | 85 | 86 | 87 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 88 | 89 | llm_chain.run(question) 90 | 91 | 92 | 93 | 94 | " Let's start with the year that Justin Beiber was born. You know that he was born in 1994. We have to go back one year. 1993.\n\n1993 was the year that the Dallas Cowboys won the Super Bowl. They won over the Buffalo Bills in Super Bowl 26.\n\nNow, let's do it backwards. According to our information, the Green Bay Packers last won the Super Bowl in the 2010-2011 season. Now, we can't go back in time, so let's go from 2011 when the Packers won the Super Bowl, back to 1984. That is the year that the Packers won the Super Bowl over the Raiders.\n\nSo, we have the year that Justin Beiber was born, 1994, and the year that the Packers last won the Super Bowl, 2011, and now we have to go in the middle, 1986. That is the year that the New York Giants won the Super Bowl over the Denver Broncos. The Giants won Super Bowl 21.\n\nThe New York Giants won the Super Bowl in 1986. This means that the Green Bay Packers won the Super Bowl in 2011.\n\nDid you get it right? If you are still a bit confused, just try to go back to the question again and review the answer" 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | previous 108 | CerebriumAI 109 | 110 | 111 | 112 | 113 | next 114 | C Transformers 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | By Harrison Chase 128 | 129 | 130 | 131 | 132 | 133 | © Copyright 2023, Harrison Chase. 134 | 135 | 136 | 137 | 138 | 139 | Last updated on Jun 13, 2023. 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /data/langchain_doc_small/33_ForefrontAI_Contents_Imports.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | ForefrontAI 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Imports 29 | Set the Environment API Key 30 | Create the ForefrontAI instance 31 | Create a Prompt Template 32 | Initiate the LLMChain 33 | Run the LLMChain 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ForefrontAI# 43 | The Forefront platform gives you the ability to fine-tune and use open source large language models. 44 | This notebook goes over how to use Langchain with ForefrontAI. 45 | 46 | Imports# 47 | 48 | 49 | import os 50 | from langchain.llms import ForefrontAI 51 | from langchain import PromptTemplate, LLMChain 52 | 53 | 54 | 55 | 56 | 57 | 58 | Set the Environment API Key# 59 | Make sure to get your API key from ForefrontAI. You are given a 5 day free trial to test different models. 60 | 61 | 62 | # get a new token: https://docs.forefront.ai/forefront/api-reference/authentication 63 | 64 | from getpass import getpass 65 | 66 | FOREFRONTAI_API_KEY = getpass() 67 | 68 | 69 | 70 | 71 | 72 | 73 | os.environ["FOREFRONTAI_API_KEY"] = FOREFRONTAI_API_KEY 74 | 75 | 76 | 77 | 78 | 79 | 80 | Create the ForefrontAI instance# 81 | You can specify different parameters such as the model endpoint url, length, temperature, etc. You must provide an endpoint url. 82 | 83 | 84 | llm = ForefrontAI(endpoint_url="YOUR ENDPOINT URL HERE") 85 | 86 | 87 | 88 | 89 | 90 | 91 | Create a Prompt Template# 92 | We will create a prompt template for Question and Answer. 93 | 94 | 95 | template = """Question: {question} 96 | 97 | Answer: Let's think step by step.""" 98 | 99 | prompt = PromptTemplate(template=template, input_variables=["question"]) 100 | 101 | 102 | 103 | 104 | 105 | 106 | Initiate the LLMChain# 107 | 108 | 109 | llm_chain = LLMChain(prompt=prompt, llm=llm) 110 | 111 | 112 | 113 | 114 | 115 | 116 | Run the LLMChain# 117 | Provide a question and run the LLMChain. 118 | 119 | 120 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 121 | 122 | llm_chain.run(question) 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | previous 137 | DeepInfra 138 | 139 | 140 | 141 | 142 | next 143 | Google Cloud Platform Vertex AI PaLM 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | Contents 154 | 155 | 156 | 157 | Imports 158 | Set the Environment API Key 159 | Create the ForefrontAI instance 160 | Create a Prompt Template 161 | Initiate the LLMChain 162 | Run the LLMChain 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | By Harrison Chase 173 | 174 | 175 | 176 | 177 | 178 | © Copyright 2023, Harrison Chase. 179 | 180 | 181 | 182 | 183 | 184 | Last updated on Jun 13, 2023. 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /data/langchain_doc_small/35_GooseAI_Contents_Install.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | GooseAI 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Install openai 29 | Imports 30 | Set the Environment API Key 31 | Create the GooseAI instance 32 | Create a Prompt Template 33 | Initiate the LLMChain 34 | Run the LLMChain 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | GooseAI# 44 | GooseAI is a fully managed NLP-as-a-Service, delivered via API. GooseAI provides access to these models. 45 | This notebook goes over how to use Langchain with GooseAI. 46 | 47 | Install openai# 48 | The openai package is required to use the GooseAI API. Install openai using pip3 install openai. 49 | 50 | 51 | $ pip3 install openai 52 | 53 | 54 | 55 | 56 | 57 | 58 | Imports# 59 | 60 | 61 | import os 62 | from langchain.llms import GooseAI 63 | from langchain import PromptTemplate, LLMChain 64 | 65 | 66 | 67 | 68 | 69 | 70 | Set the Environment API Key# 71 | Make sure to get your API key from GooseAI. You are given $10 in free credits to test different models. 72 | 73 | 74 | from getpass import getpass 75 | 76 | GOOSEAI_API_KEY = getpass() 77 | 78 | 79 | 80 | 81 | 82 | 83 | os.environ["GOOSEAI_API_KEY"] = GOOSEAI_API_KEY 84 | 85 | 86 | 87 | 88 | 89 | 90 | Create the GooseAI instance# 91 | You can specify different parameters such as the model name, max tokens generated, temperature, etc. 92 | 93 | 94 | llm = GooseAI() 95 | 96 | 97 | 98 | 99 | 100 | 101 | Create a Prompt Template# 102 | We will create a prompt template for Question and Answer. 103 | 104 | 105 | template = """Question: {question} 106 | 107 | Answer: Let's think step by step.""" 108 | 109 | prompt = PromptTemplate(template=template, input_variables=["question"]) 110 | 111 | 112 | 113 | 114 | 115 | 116 | Initiate the LLMChain# 117 | 118 | 119 | llm_chain = LLMChain(prompt=prompt, llm=llm) 120 | 121 | 122 | 123 | 124 | 125 | 126 | Run the LLMChain# 127 | Provide a question and run the LLMChain. 128 | 129 | 130 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 131 | 132 | llm_chain.run(question) 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | previous 147 | Google Cloud Platform Vertex AI PaLM 148 | 149 | 150 | 151 | 152 | next 153 | GPT4All 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | Contents 164 | 165 | 166 | 167 | Install openai 168 | Imports 169 | Set the Environment API Key 170 | Create the GooseAI instance 171 | Create a Prompt Template 172 | Initiate the LLMChain 173 | Run the LLMChain 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | By Harrison Chase 184 | 185 | 186 | 187 | 188 | 189 | © Copyright 2023, Harrison Chase. 190 | 191 | 192 | 193 | 194 | 195 | Last updated on Jun 13, 2023. 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | -------------------------------------------------------------------------------- /data/langchain_doc_small/26_Beam_Beam_Beam.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Beam 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Beam# 27 | 28 | Beam makes it easy to run code on GPUs, deploy scalable web APIs, 29 | schedule cron jobs, and run massively parallel workloads — without managing any infrastructure. 30 | 31 | Calls the Beam API wrapper to deploy and make subsequent calls to an instance of the gpt2 LLM in a cloud deployment. Requires installation of the Beam library and registration of Beam Client ID and Client Secret. By calling the wrapper an instance of the model is created and run, with returned text relating to the prompt. Additional calls can then be made by directly calling the Beam API. 32 | Create an account, if you don’t have one already. Grab your API keys from the dashboard. 33 | Install the Beam CLI 34 | 35 | 36 | !curl https://raw.githubusercontent.com/slai-labs/get-beam/main/get-beam.sh -sSfL | sh 37 | 38 | 39 | 40 | 41 | Register API Keys and set your beam client id and secret environment variables: 42 | 43 | 44 | import os 45 | import subprocess 46 | 47 | beam_client_id = "" 48 | beam_client_secret = "" 49 | 50 | # Set the environment variables 51 | os.environ['BEAM_CLIENT_ID'] = beam_client_id 52 | os.environ['BEAM_CLIENT_SECRET'] = beam_client_secret 53 | 54 | # Run the beam configure command 55 | !beam configure --clientId={beam_client_id} --clientSecret={beam_client_secret} 56 | 57 | 58 | 59 | 60 | Install the Beam SDK: 61 | 62 | 63 | !pip install beam-sdk 64 | 65 | 66 | 67 | 68 | Deploy and call Beam directly from langchain! 69 | Note that a cold start might take a couple of minutes to return the response, but subsequent calls will be faster! 70 | 71 | 72 | from langchain.llms.beam import Beam 73 | 74 | llm = Beam(model_name="gpt2", 75 | name="langchain-gpt2-test", 76 | cpu=8, 77 | memory="32Gi", 78 | gpu="A10G", 79 | python_version="python3.8", 80 | python_packages=[ 81 | "diffusers[torch]>=0.10", 82 | "transformers", 83 | "torch", 84 | "pillow", 85 | "accelerate", 86 | "safetensors", 87 | "xformers",], 88 | max_length="50", 89 | verbose=False) 90 | 91 | llm._deploy() 92 | 93 | response = llm._call("Running machine learning on a remote GPU") 94 | 95 | print(response) 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | previous 109 | Baseten 110 | 111 | 112 | 113 | 114 | next 115 | Bedrock 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | By Harrison Chase 129 | 130 | 131 | 132 | 133 | 134 | © Copyright 2023, Harrison Chase. 135 | 136 | 137 | 138 | 139 | 140 | Last updated on Jun 13, 2023. 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /data/langchain_doc_small/28_CerebriumAI_Contents_Install.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | CerebriumAI 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Install cerebrium 29 | Imports 30 | Set the Environment API Key 31 | Create the CerebriumAI instance 32 | Create a Prompt Template 33 | Initiate the LLMChain 34 | Run the LLMChain 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | CerebriumAI# 44 | Cerebrium is an AWS Sagemaker alternative. It also provides API access to several LLM models. 45 | This notebook goes over how to use Langchain with CerebriumAI. 46 | 47 | Install cerebrium# 48 | The cerebrium package is required to use the CerebriumAI API. Install cerebrium using pip3 install cerebrium. 49 | 50 | 51 | # Install the package 52 | !pip3 install cerebrium 53 | 54 | 55 | 56 | 57 | 58 | 59 | Imports# 60 | 61 | 62 | import os 63 | from langchain.llms import CerebriumAI 64 | from langchain import PromptTemplate, LLMChain 65 | 66 | 67 | 68 | 69 | 70 | 71 | Set the Environment API Key# 72 | Make sure to get your API key from CerebriumAI. See here. You are given a 1 hour free of serverless GPU compute to test different models. 73 | 74 | 75 | os.environ["CEREBRIUMAI_API_KEY"] = "YOUR_KEY_HERE" 76 | 77 | 78 | 79 | 80 | 81 | 82 | Create the CerebriumAI instance# 83 | You can specify different parameters such as the model endpoint url, max length, temperature, etc. You must provide an endpoint url. 84 | 85 | 86 | llm = CerebriumAI(endpoint_url="YOUR ENDPOINT URL HERE") 87 | 88 | 89 | 90 | 91 | 92 | 93 | Create a Prompt Template# 94 | We will create a prompt template for Question and Answer. 95 | 96 | 97 | template = """Question: {question} 98 | 99 | Answer: Let's think step by step.""" 100 | 101 | prompt = PromptTemplate(template=template, input_variables=["question"]) 102 | 103 | 104 | 105 | 106 | 107 | 108 | Initiate the LLMChain# 109 | 110 | 111 | llm_chain = LLMChain(prompt=prompt, llm=llm) 112 | 113 | 114 | 115 | 116 | 117 | 118 | Run the LLMChain# 119 | Provide a question and run the LLMChain. 120 | 121 | 122 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 123 | 124 | llm_chain.run(question) 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | previous 139 | Bedrock 140 | 141 | 142 | 143 | 144 | next 145 | Cohere 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | Contents 156 | 157 | 158 | 159 | Install cerebrium 160 | Imports 161 | Set the Environment API Key 162 | Create the CerebriumAI instance 163 | Create a Prompt Template 164 | Initiate the LLMChain 165 | Run the LLMChain 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | By Harrison Chase 176 | 177 | 178 | 179 | 180 | 181 | © Copyright 2023, Harrison Chase. 182 | 183 | 184 | 185 | 186 | 187 | Last updated on Jun 13, 2023. 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | -------------------------------------------------------------------------------- /data/langchain_doc_small/6_Getting_Started_Contents.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Getting Started 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | Language Models 27 | text -> text interface 28 | messages -> message interface 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Getting Started# 40 | One of the core value props of LangChain is that it provides a standard interface to models. This allows you to swap easily between models. At a high level, there are two main types of models: 41 | 42 | Language Models: good for text generation 43 | Text Embedding Models: good for turning text into a numerical representation 44 | 45 | 46 | Language Models# 47 | There are two different sub-types of Language Models: 48 | 49 | LLMs: these wrap APIs which take text in and return text 50 | ChatModels: these wrap models which take chat messages in and return a chat message 51 | 52 | This is a subtle difference, but a value prop of LangChain is that we provide a unified interface accross these. This is nice because although the underlying APIs are actually quite different, you often want to use them interchangeably. 53 | To see this, let’s look at OpenAI (a wrapper around OpenAI’s LLM) vs ChatOpenAI (a wrapper around OpenAI’s ChatModel). 54 | 55 | 56 | from langchain.llms import OpenAI 57 | from langchain.chat_models import ChatOpenAI 58 | 59 | 60 | 61 | 62 | 63 | 64 | llm = OpenAI() 65 | 66 | 67 | 68 | 69 | 70 | 71 | chat_model = ChatOpenAI() 72 | 73 | 74 | 75 | 76 | 77 | text -> text interface# 78 | 79 | 80 | llm.predict("say hi!") 81 | 82 | 83 | 84 | 85 | '\n\nHi there!' 86 | 87 | 88 | 89 | 90 | 91 | 92 | chat_model.predict("say hi!") 93 | 94 | 95 | 96 | 97 | 'Hello there!' 98 | 99 | 100 | 101 | 102 | 103 | 104 | messages -> message interface# 105 | 106 | 107 | from langchain.schema import HumanMessage 108 | 109 | 110 | 111 | 112 | 113 | 114 | llm.predict_messages([HumanMessage(content="say hi!")]) 115 | 116 | 117 | 118 | 119 | AIMessage(content='\n\nHello! Nice to meet you!', additional_kwargs={}, example=False) 120 | 121 | 122 | 123 | 124 | 125 | 126 | chat_model.predict_messages([HumanMessage(content="say hi!")]) 127 | 128 | 129 | 130 | 131 | AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False) 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | previous 147 | Models 148 | 149 | 150 | 151 | 152 | next 153 | LLMs 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | Contents 164 | 165 | 166 | 167 | Language Models 168 | text -> text interface 169 | messages -> message interface 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | By Harrison Chase 182 | 183 | 184 | 185 | 186 | 187 | © Copyright 2023, Harrison Chase. 188 | 189 | 190 | 191 | 192 | 193 | Last updated on Jun 14, 2023. 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | -------------------------------------------------------------------------------- /data/langchain_doc_small/34_Google_Cloud_Platform.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Google Cloud Platform Vertex AI PaLM 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Google Cloud Platform Vertex AI PaLM# 27 | Note: This is seperate from the Google PaLM integration. Google has chosen to offer an enterprise version of PaLM through GCP, and this supports the models made available through there. 28 | PaLM API on Vertex AI is a Preview offering, subject to the Pre-GA Offerings Terms of the GCP Service Specific Terms. 29 | Pre-GA products and features may have limited support, and changes to pre-GA products and features may not be compatible with other pre-GA versions. For more information, see the launch stage descriptions. Further, by using PaLM API on Vertex AI, you agree to the Generative AI Preview terms and conditions (Preview Terms). 30 | For PaLM API on Vertex AI, you can process personal data as outlined in the Cloud Data Processing Addendum, subject to applicable restrictions and obligations in the Agreement (as defined in the Preview Terms). 31 | To use Vertex AI PaLM you must have the google-cloud-aiplatform Python package installed and either: 32 | 33 | Have credentials configured for your environment (gcloud, workload identity, etc…) 34 | Store the path to a service account JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable 35 | 36 | This codebase uses the google.auth library which first looks for the application credentials variable mentioned above, and then looks for system-level auth. 37 | For more information, see: 38 | 39 | https://cloud.google.com/docs/authentication/application-default-credentials#GAC 40 | https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth 41 | 42 | 43 | 44 | #!pip install google-cloud-aiplatform 45 | 46 | 47 | 48 | 49 | 50 | 51 | from langchain.llms import VertexAI 52 | from langchain import PromptTemplate, LLMChain 53 | 54 | 55 | 56 | 57 | 58 | 59 | template = """Question: {question} 60 | 61 | Answer: Let's think step by step.""" 62 | 63 | prompt = PromptTemplate(template=template, input_variables=["question"]) 64 | 65 | 66 | 67 | 68 | 69 | 70 | llm = VertexAI() 71 | 72 | 73 | 74 | 75 | 76 | 77 | llm_chain = LLMChain(prompt=prompt, llm=llm) 78 | 79 | 80 | 81 | 82 | 83 | 84 | question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" 85 | 86 | llm_chain.run(question) 87 | 88 | 89 | 90 | 91 | 'Justin Bieber was born on March 1, 1994. The Super Bowl in 1994 was won by the San Francisco 49ers.\nThe final answer: San Francisco 49ers.' 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | previous 105 | ForefrontAI 106 | 107 | 108 | 109 | 110 | next 111 | GooseAI 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | By Harrison Chase 125 | 126 | 127 | 128 | 129 | 130 | © Copyright 2023, Harrison Chase. 131 | 132 | 133 | 134 | 135 | 136 | Last updated on Jun 13, 2023. 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /data/langchain_doc_small/32_DeepInfra_Contents_Imports.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | DeepInfra 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Imports 29 | Set the Environment API Key 30 | Create the DeepInfra instance 31 | Create a Prompt Template 32 | Initiate the LLMChain 33 | Run the LLMChain 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | DeepInfra# 43 | DeepInfra provides several LLMs. 44 | This notebook goes over how to use Langchain with DeepInfra. 45 | 46 | Imports# 47 | 48 | 49 | import os 50 | from langchain.llms import DeepInfra 51 | from langchain import PromptTemplate, LLMChain 52 | 53 | 54 | 55 | 56 | 57 | 58 | Set the Environment API Key# 59 | Make sure to get your API key from DeepInfra. You have to Login and get a new token. 60 | You are given a 1 hour free of serverless GPU compute to test different models. (see here) 61 | You can print your token with deepctl auth token 62 | 63 | 64 | # get a new token: https://deepinfra.com/login?from=%2Fdash 65 | 66 | from getpass import getpass 67 | 68 | DEEPINFRA_API_TOKEN = getpass() 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN 78 | 79 | 80 | 81 | 82 | 83 | 84 | Create the DeepInfra instance# 85 | You can also use our open source deepctl tool to manage your model deployments. You can view a list of available parameters here. 86 | 87 | 88 | llm = DeepInfra(model_id="databricks/dolly-v2-12b") 89 | llm.model_kwargs = {'temperature': 0.7, 'repetition_penalty': 1.2, 'max_new_tokens': 250, 'top_p': 0.9} 90 | 91 | 92 | 93 | 94 | 95 | 96 | Create a Prompt Template# 97 | We will create a prompt template for Question and Answer. 98 | 99 | 100 | template = """Question: {question} 101 | 102 | Answer: Let's think step by step.""" 103 | 104 | prompt = PromptTemplate(template=template, input_variables=["question"]) 105 | 106 | 107 | 108 | 109 | 110 | 111 | Initiate the LLMChain# 112 | 113 | 114 | llm_chain = LLMChain(prompt=prompt, llm=llm) 115 | 116 | 117 | 118 | 119 | 120 | 121 | Run the LLMChain# 122 | Provide a question and run the LLMChain. 123 | 124 | 125 | question = "Can penguins reach the North pole?" 126 | 127 | llm_chain.run(question) 128 | 129 | 130 | 131 | 132 | "Penguins live in the Southern hemisphere.\nThe North pole is located in the Northern hemisphere.\nSo, first you need to turn the penguin South.\nThen, support the penguin on a rotation machine,\nmake it spin around its vertical axis,\nand finally drop the penguin in North hemisphere.\nNow, you have a penguin in the north pole!\n\nStill didn't understand?\nWell, you're a failure as a teacher." 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | previous 147 | Databricks 148 | 149 | 150 | 151 | 152 | next 153 | ForefrontAI 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | Contents 164 | 165 | 166 | 167 | Imports 168 | Set the Environment API Key 169 | Create the DeepInfra instance 170 | Create a Prompt Template 171 | Initiate the LLMChain 172 | Run the LLMChain 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | By Harrison Chase 183 | 184 | 185 | 186 | 187 | 188 | © Copyright 2023, Harrison Chase. 189 | 190 | 191 | 192 | 193 | 194 | Last updated on Jun 13, 2023. 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /data/langchain_doc_small/10_How_to_use.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | How to use the async API for LLMs 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | How to use the async API for LLMs# 25 | LangChain provides async support for LLMs by leveraging the asyncio library. 26 | Async support is particularly useful for calling multiple LLMs concurrently, as these calls are network-bound. Currently, OpenAI, PromptLayerOpenAI, ChatOpenAI and Anthropic are supported, but async support for other LLMs is on the roadmap. 27 | You can use the agenerate method to call an OpenAI LLM asynchronously. 28 | 29 | 30 | import time 31 | import asyncio 32 | 33 | from langchain.llms import OpenAI 34 | 35 | def generate_serially(): 36 | llm = OpenAI(temperature=0.9) 37 | for _ in range(10): 38 | resp = llm.generate(["Hello, how are you?"]) 39 | print(resp.generations[0][0].text) 40 | 41 | 42 | async def async_generate(llm): 43 | resp = await llm.agenerate(["Hello, how are you?"]) 44 | print(resp.generations[0][0].text) 45 | 46 | 47 | async def generate_concurrently(): 48 | llm = OpenAI(temperature=0.9) 49 | tasks = [async_generate(llm) for _ in range(10)] 50 | await asyncio.gather(*tasks) 51 | 52 | 53 | s = time.perf_counter() 54 | # If running this outside of Jupyter, use asyncio.run(generate_concurrently()) 55 | await generate_concurrently() 56 | elapsed = time.perf_counter() - s 57 | print('\033[1m' + f"Concurrent executed in {elapsed:0.2f} seconds." + '\033[0m') 58 | 59 | s = time.perf_counter() 60 | generate_serially() 61 | elapsed = time.perf_counter() - s 62 | print('\033[1m' + f"Serial executed in {elapsed:0.2f} seconds." + '\033[0m') 63 | 64 | 65 | 66 | 67 | I'm doing well, thank you. How about you? 68 | 69 | 70 | I'm doing well, thank you. How about you? 71 | 72 | 73 | I'm doing well, how about you? 74 | 75 | 76 | I'm doing well, thank you. How about you? 77 | 78 | 79 | I'm doing well, thank you. How about you? 80 | 81 | 82 | I'm doing well, thank you. How about yourself? 83 | 84 | 85 | I'm doing well, thank you! How about you? 86 | 87 | 88 | I'm doing well, thank you. How about you? 89 | 90 | 91 | I'm doing well, thank you! How about you? 92 | 93 | 94 | I'm doing well, thank you. How about you? 95 | Concurrent executed in 1.39 seconds. 96 | 97 | 98 | I'm doing well, thank you. How about you? 99 | 100 | 101 | I'm doing well, thank you. How about you? 102 | 103 | I'm doing well, thank you. How about you? 104 | 105 | 106 | I'm doing well, thank you. How about you? 107 | 108 | 109 | I'm doing well, thank you. How about yourself? 110 | 111 | 112 | I'm doing well, thanks for asking. How about you? 113 | 114 | 115 | I'm doing well, thanks! How about you? 116 | 117 | 118 | I'm doing well, thank you. How about you? 119 | 120 | 121 | I'm doing well, thank you. How about yourself? 122 | 123 | 124 | I'm doing well, thanks for asking. How about you? 125 | Serial executed in 5.77 seconds. 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | previous 139 | Generic Functionality 140 | 141 | 142 | 143 | 144 | next 145 | How to write a custom LLM wrapper 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | By Harrison Chase 159 | 160 | 161 | 162 | 163 | 164 | © Copyright 2023, Harrison Chase. 165 | 166 | 167 | 168 | 169 | 170 | Last updated on Jun 14, 2023. 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /data/langchain_doc_small/38_Hugging_Face_Pipeline.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Hugging Face Pipeline 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Load the model 29 | Integrate the model in an LLMChain 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Hugging Face Pipeline# 39 | Hugging Face models can be run locally through the HuggingFacePipeline class. 40 | The Hugging Face Model Hub hosts over 120k models, 20k datasets, and 50k demo apps (Spaces), all open source and publicly available, in an online platform where people can easily collaborate and build ML together. 41 | These can be called from LangChain either through this local pipeline wrapper or by calling their hosted inference endpoints through the HuggingFaceHub class. For more information on the hosted pipelines, see the HuggingFaceHub notebook. 42 | To use, you should have the transformers python package installed. 43 | 44 | 45 | !pip install transformers > /dev/null 46 | 47 | 48 | 49 | 50 | 51 | Load the model# 52 | 53 | 54 | from langchain import HuggingFacePipeline 55 | 56 | llm = HuggingFacePipeline.from_model_id(model_id="bigscience/bloom-1b7", task="text-generation", model_kwargs={"temperature":0, "max_length":64}) 57 | 58 | 59 | 60 | 61 | WARNING:root:Failed to default session, using empty session: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /sessions (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused')) 62 | 63 | 64 | 65 | 66 | 67 | 68 | Integrate the model in an LLMChain# 69 | 70 | 71 | from langchain import PromptTemplate, LLMChain 72 | 73 | template = """Question: {question} 74 | 75 | Answer: Let's think step by step.""" 76 | prompt = PromptTemplate(template=template, input_variables=["question"]) 77 | 78 | llm_chain = LLMChain(prompt=prompt, llm=llm) 79 | 80 | question = "What is electroencephalography?" 81 | 82 | print(llm_chain.run(question)) 83 | 84 | 85 | 86 | 87 | /Users/wfh/code/lc/lckg/.venv/lib/python3.11/site-packages/transformers/generation/utils.py:1288: UserWarning: Using `max_length`'s default (64) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation. 88 | warnings.warn( 89 | WARNING:root:Failed to persist run: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /chain-runs (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused')) 90 | 91 | 92 | First, we need to understand what is an electroencephalogram. An electroencephalogram is a recording of brain activity. It is a recording of brain activity that is made by placing electrodes on the scalp. The electrodes are placed 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | previous 107 | Hugging Face Hub 108 | 109 | 110 | 111 | 112 | next 113 | Huggingface TextGen Inference 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | Contents 124 | 125 | 126 | 127 | Load the model 128 | Integrate the model in an LLMChain 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | By Harrison Chase 139 | 140 | 141 | 142 | 143 | 144 | © Copyright 2023, Harrison Chase. 145 | 146 | 147 | 148 | 149 | 150 | Last updated on Jun 13, 2023. 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /data/langchain_doc_small/3_Concepts_Contents_Chain.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Concepts 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | Chain of Thought 27 | Action Plan Generation 28 | ReAct 29 | Self-ask 30 | Prompt Chaining 31 | Memetic Proxy 32 | Self Consistency 33 | Inception 34 | MemPrompt 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Concepts# 44 | These are concepts and terminology commonly used when developing LLM applications. 45 | It contains reference to external papers or sources where the concept was first introduced, 46 | as well as to places in LangChain where the concept is used. 47 | 48 | Chain of Thought# 49 | Chain of Thought (CoT) is a prompting technique used to encourage the model to generate a series of intermediate reasoning steps. 50 | A less formal way to induce this behavior is to include “Let’s think step-by-step” in the prompt. 51 | 52 | Chain-of-Thought Paper 53 | Step-by-Step Paper 54 | 55 | 56 | 57 | Action Plan Generation# 58 | Action Plan Generation is a prompting technique that uses a language model to generate actions to take. 59 | The results of these actions can then be fed back into the language model to generate a subsequent action. 60 | 61 | WebGPT Paper 62 | SayCan Paper 63 | 64 | 65 | 66 | ReAct# 67 | ReAct is a prompting technique that combines Chain-of-Thought prompting with action plan generation. 68 | This induces the model to think about what action to take, then take it. 69 | 70 | Paper 71 | LangChain Example 72 | 73 | 74 | 75 | Self-ask# 76 | Self-ask is a prompting method that builds on top of chain-of-thought prompting. 77 | In this method, the model explicitly asks itself follow-up questions, which are then answered by an external search engine. 78 | 79 | Paper 80 | LangChain Example 81 | 82 | 83 | 84 | Prompt Chaining# 85 | Prompt Chaining is combining multiple LLM calls, with the output of one-step being the input to the next. 86 | 87 | PromptChainer Paper 88 | Language Model Cascades 89 | ICE Primer Book 90 | Socratic Models 91 | 92 | 93 | 94 | Memetic Proxy# 95 | Memetic Proxy is encouraging the LLM 96 | to respond in a certain way framing the discussion in a context that the model knows of and that 97 | will result in that type of response. 98 | For example, as a conversation between a student and a teacher. 99 | 100 | Paper 101 | 102 | 103 | 104 | Self Consistency# 105 | Self Consistency is a decoding strategy that samples a diverse set of reasoning paths and then selects the most consistent answer. 106 | Is most effective when combined with Chain-of-thought prompting. 107 | 108 | Paper 109 | 110 | 111 | 112 | Inception# 113 | Inception is also called First Person Instruction. 114 | It is encouraging the model to think a certain way by including the start of the model’s response in the prompt. 115 | 116 | Example 117 | 118 | 119 | 120 | MemPrompt# 121 | MemPrompt maintains a memory of errors and user feedback, and uses them to prevent repetition of mistakes. 122 | 123 | Paper 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | previous 135 | Quickstart Guide 136 | 137 | 138 | 139 | 140 | next 141 | Tutorials 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | Contents 152 | 153 | 154 | 155 | Chain of Thought 156 | Action Plan Generation 157 | ReAct 158 | Self-ask 159 | Prompt Chaining 160 | Memetic Proxy 161 | Self Consistency 162 | Inception 163 | MemPrompt 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | By Harrison Chase 174 | 175 | 176 | 177 | 178 | 179 | © Copyright 2023, Harrison Chase. 180 | 181 | 182 | 183 | 184 | 185 | Last updated on Jun 14, 2023. 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /data/langchain_doc_small/23_Azure_OpenAI_Contents.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | Azure OpenAI 21 | 22 | 23 | 24 | 25 | Contents 26 | 27 | 28 | 29 | API configuration 30 | Deployments 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Azure OpenAI# 40 | This notebook goes over how to use Langchain with Azure OpenAI. 41 | The Azure OpenAI API is compatible with OpenAI’s API. The openai Python package makes it easy to use both OpenAI and Azure OpenAI. You can call Azure OpenAI the same way you call OpenAI with the exceptions noted below. 42 | 43 | API configuration# 44 | You can configure the openai package to use Azure OpenAI using environment variables. The following is for bash: 45 | # Set this to `azure` 46 | export OPENAI_API_TYPE=azure 47 | # The API version you want to use: set this to `2023-03-15-preview` for the released version. 48 | export OPENAI_API_VERSION=2023-03-15-preview 49 | # The base URL for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource. 50 | export OPENAI_API_BASE=https://your-resource-name.openai.azure.com 51 | # The API key for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource. 52 | export OPENAI_API_KEY= 53 | 54 | 55 | Alternatively, you can configure the API right within your running Python environment: 56 | import os 57 | os.environ["OPENAI_API_TYPE"] = "azure" 58 | ... 59 | 60 | 61 | 62 | 63 | Deployments# 64 | With Azure OpenAI, you set up your own deployments of the common GPT-3 and Codex models. When calling the API, you need to specify the deployment you want to use. 65 | Let’s say your deployment name is text-davinci-002-prod. In the openai Python API, you can specify this deployment with the engine parameter. For example: 66 | import openai 67 | 68 | response = openai.Completion.create( 69 | engine="text-davinci-002-prod", 70 | prompt="This is a test", 71 | max_tokens=5 72 | ) 73 | 74 | 75 | 76 | 77 | !pip install openai 78 | 79 | 80 | 81 | 82 | 83 | 84 | import os 85 | os.environ["OPENAI_API_TYPE"] = "azure" 86 | os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview" 87 | os.environ["OPENAI_API_BASE"] = "..." 88 | os.environ["OPENAI_API_KEY"] = "..." 89 | 90 | 91 | 92 | 93 | 94 | 95 | # Import Azure OpenAI 96 | from langchain.llms import AzureOpenAI 97 | 98 | 99 | 100 | 101 | 102 | 103 | # Create an instance of Azure OpenAI 104 | # Replace the deployment name with your own 105 | llm = AzureOpenAI( 106 | deployment_name="td2", 107 | model_name="text-davinci-002", 108 | ) 109 | 110 | 111 | 112 | 113 | 114 | 115 | # Run the LLM 116 | llm("Tell me a joke") 117 | 118 | 119 | 120 | 121 | "\n\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!" 122 | 123 | 124 | 125 | 126 | We can also print the LLM and see its custom print. 127 | 128 | 129 | print(llm) 130 | 131 | 132 | 133 | 134 | AzureOpenAI 135 | Params: {'deployment_name': 'text-davinci-002', 'model_name': 'text-davinci-002', 'temperature': 0.7, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | previous 150 | Aviary 151 | 152 | 153 | 154 | 155 | next 156 | Banana 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | Contents 167 | 168 | 169 | 170 | API configuration 171 | Deployments 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | By Harrison Chase 182 | 183 | 184 | 185 | 186 | 187 | © Copyright 2023, Harrison Chase. 188 | 189 | 190 | 191 | 192 | 193 | Last updated on Jun 13, 2023. 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | -------------------------------------------------------------------------------- /data/langchain_doc_small/25_Baseten_Contents_Baseten.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | Baseten 21 | 22 | 23 | 24 | 25 | Contents 26 | 27 | 28 | 29 | Baseten 30 | Setup 31 | Single model call 32 | Chained model calls 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Baseten# 42 | Baseten provides all the infrastructure you need to deploy and serve ML models performantly, scalably, and cost-efficiently. 43 | This example demonstrates using Langchain with models deployed on Baseten. 44 | 45 | 46 | Setup# 47 | To run this notebook, you’ll need a Baseten account and an API key. 48 | You’ll also need to install the Baseten Python package: 49 | 50 | 51 | !pip install baseten 52 | 53 | 54 | 55 | 56 | 57 | 58 | import baseten 59 | 60 | baseten.login("YOUR_API_KEY") 61 | 62 | 63 | 64 | 65 | 66 | 67 | Single model call# 68 | First, you’ll need to deploy a model to Baseten. 69 | You can deploy foundation models like WizardLM and Alpaca with one click from the Baseten model library or if you have your own model, deploy it with this tutorial. 70 | In this example, we’ll work with WizardLM. Deploy WizardLM here and follow along with the deployed model’s version ID. 71 | 72 | 73 | from langchain.llms import Baseten 74 | 75 | 76 | 77 | 78 | 79 | 80 | # Load the model 81 | wizardlm = Baseten(model="MODEL_VERSION_ID", verbose=True) 82 | 83 | 84 | 85 | 86 | 87 | 88 | # Prompt the model 89 | 90 | wizardlm("What is the difference between a Wizard and a Sorcerer?") 91 | 92 | 93 | 94 | 95 | 96 | 97 | Chained model calls# 98 | We can chain together multiple calls to one or multiple models, which is the whole point of Langchain! 99 | This example uses WizardLM to plan a meal with an entree, three sides, and an alcoholic and non-alcoholic beverage pairing. 100 | 101 | 102 | from langchain.chains import SimpleSequentialChain 103 | from langchain import PromptTemplate, LLMChain 104 | 105 | 106 | 107 | 108 | 109 | 110 | # Build the first link in the chain 111 | 112 | prompt = PromptTemplate( 113 | input_variables=["cuisine"], 114 | template="Name a complex entree for a {cuisine} dinner. Respond with just the name of a single dish.", 115 | ) 116 | 117 | link_one = LLMChain(llm=wizardlm, prompt=prompt) 118 | 119 | 120 | 121 | 122 | 123 | 124 | # Build the second link in the chain 125 | 126 | prompt = PromptTemplate( 127 | input_variables=["entree"], 128 | template="What are three sides that would go with {entree}. Respond with only a list of the sides.", 129 | ) 130 | 131 | link_two = LLMChain(llm=wizardlm, prompt=prompt) 132 | 133 | 134 | 135 | 136 | 137 | 138 | # Build the third link in the chain 139 | 140 | prompt = PromptTemplate( 141 | input_variables=["sides"], 142 | template="What is one alcoholic and one non-alcoholic beverage that would go well with this list of sides: {sides}. Respond with only the names of the beverages.", 143 | ) 144 | 145 | link_three = LLMChain(llm=wizardlm, prompt=prompt) 146 | 147 | 148 | 149 | 150 | 151 | 152 | # Run the full chain! 153 | 154 | menu_maker = SimpleSequentialChain(chains=[link_one, link_two, link_three], verbose=True) 155 | menu_maker.run("South Indian") 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | previous 169 | Banana 170 | 171 | 172 | 173 | 174 | next 175 | Beam 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | Contents 186 | 187 | 188 | 189 | Baseten 190 | Setup 191 | Single model call 192 | Chained model calls 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | By Harrison Chase 203 | 204 | 205 | 206 | 207 | 208 | © Copyright 2023, Harrison Chase. 209 | 210 | 211 | 212 | 213 | 214 | Last updated on Jun 13, 2023. 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /data/langchain_doc_small/17_How_to_track.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | How to track token usage 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | How to track token usage# 27 | This notebook goes over how to track your token usage for specific calls. It is currently only implemented for the OpenAI API. 28 | Let’s first look at an extremely simple example of tracking token usage for a single LLM call. 29 | 30 | 31 | from langchain.llms import OpenAI 32 | from langchain.callbacks import get_openai_callback 33 | 34 | 35 | 36 | 37 | 38 | 39 | llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2) 40 | 41 | 42 | 43 | 44 | 45 | 46 | with get_openai_callback() as cb: 47 | result = llm("Tell me a joke") 48 | print(cb) 49 | 50 | 51 | 52 | 53 | Tokens Used: 42 54 | Prompt Tokens: 4 55 | Completion Tokens: 38 56 | Successful Requests: 1 57 | Total Cost (USD): $0.00084 58 | 59 | 60 | 61 | 62 | Anything inside the context manager will get tracked. Here’s an example of using it to track multiple calls in sequence. 63 | 64 | 65 | with get_openai_callback() as cb: 66 | result = llm("Tell me a joke") 67 | result2 = llm("Tell me a joke") 68 | print(cb.total_tokens) 69 | 70 | 71 | 72 | 73 | 91 74 | 75 | 76 | 77 | 78 | If a chain or agent with multiple steps in it is used, it will track all those steps. 79 | 80 | 81 | from langchain.agents import load_tools 82 | from langchain.agents import initialize_agent 83 | from langchain.agents import AgentType 84 | from langchain.llms import OpenAI 85 | 86 | llm = OpenAI(temperature=0) 87 | tools = load_tools(["serpapi", "llm-math"], llm=llm) 88 | agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 89 | 90 | 91 | 92 | 93 | 94 | 95 | with get_openai_callback() as cb: 96 | response = agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?") 97 | print(f"Total Tokens: {cb.total_tokens}") 98 | print(f"Prompt Tokens: {cb.prompt_tokens}") 99 | print(f"Completion Tokens: {cb.completion_tokens}") 100 | print(f"Total Cost (USD): ${cb.total_cost}") 101 | 102 | 103 | 104 | 105 | > Entering new AgentExecutor chain... 106 | I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power. 107 | Action: Search 108 | Action Input: "Olivia Wilde boyfriend" 109 | Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. 110 | Thought: I need to find out Harry Styles' age. 111 | Action: Search 112 | Action Input: "Harry Styles age" 113 | Observation: 29 years 114 | Thought: I need to calculate 29 raised to the 0.23 power. 115 | Action: Calculator 116 | Action Input: 29^0.23 117 | Observation: Answer: 2.169459462491557 118 | 119 | Thought: I now know the final answer. 120 | Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 29 years old and his age raised to the 0.23 power is 2.169459462491557. 121 | 122 | > Finished chain. 123 | Total Tokens: 1506 124 | Prompt Tokens: 1350 125 | Completion Tokens: 156 126 | Total Cost (USD): $0.03012 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | previous 140 | How to stream LLM and Chat Model responses 141 | 142 | 143 | 144 | 145 | next 146 | Integrations 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | By Harrison Chase 160 | 161 | 162 | 163 | 164 | 165 | © Copyright 2023, Harrison Chase. 166 | 167 | 168 | 169 | 170 | 171 | Last updated on Jun 13, 2023. 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /data/langchain_doc_small/36_GPT4All_Contents_Specify.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | GPT4All 21 | 22 | 23 | 24 | 25 | Contents 26 | 27 | 28 | 29 | Specify Model 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | GPT4All# 39 | GitHub:nomic-ai/gpt4all an ecosystem of open-source chatbots trained on a massive collections of clean assistant data including code, stories and dialogue. 40 | This example goes over how to use LangChain to interact with GPT4All models. 41 | 42 | 43 | %pip install gpt4all > /dev/null 44 | 45 | 46 | 47 | 48 | Note: you may need to restart the kernel to use updated packages. 49 | 50 | 51 | 52 | 53 | 54 | 55 | from langchain import PromptTemplate, LLMChain 56 | from langchain.llms import GPT4All 57 | from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler 58 | 59 | 60 | 61 | 62 | 63 | 64 | template = """Question: {question} 65 | 66 | Answer: Let's think step by step.""" 67 | 68 | prompt = PromptTemplate(template=template, input_variables=["question"]) 69 | 70 | 71 | 72 | 73 | 74 | Specify Model# 75 | To run locally, download a compatible ggml-formatted model. For more info, visit https://github.com/nomic-ai/gpt4all 76 | For full installation instructions go here. 77 | The GPT4All Chat installer needs to decompress a 3GB LLM model during the installation process! 78 | Note that new models are uploaded regularly - check the link above for the most recent .bin URL 79 | 80 | 81 | local_path = './models/ggml-gpt4all-l13b-snoozy.bin' # replace with your desired local file path 82 | 83 | 84 | 85 | 86 | Uncomment the below block to download a model. You may want to update url to a new version. 87 | 88 | 89 | # import requests 90 | 91 | # from pathlib import Path 92 | # from tqdm import tqdm 93 | 94 | # Path(local_path).parent.mkdir(parents=True, exist_ok=True) 95 | 96 | # # Example model. Check https://github.com/nomic-ai/gpt4all for the latest models. 97 | # url = 'http://gpt4all.io/models/ggml-gpt4all-l13b-snoozy.bin' 98 | 99 | # # send a GET request to the URL to download the file. Stream since it's large 100 | # response = requests.get(url, stream=True) 101 | 102 | # # open the file in binary mode and write the contents of the response to it in chunks 103 | # # This is a large file, so be prepared to wait. 104 | # with open(local_path, 'wb') as f: 105 | # for chunk in tqdm(response.iter_content(chunk_size=8192)): 106 | # if chunk: 107 | # f.write(chunk) 108 | 109 | 110 | 111 | 112 | 113 | 114 | # Callbacks support token-wise streaming 115 | callbacks = [StreamingStdOutCallbackHandler()] 116 | # Verbose is required to pass to the callback manager 117 | llm = GPT4All(model=local_path, callbacks=callbacks, verbose=True) 118 | # If you want to use a custom model add the backend parameter 119 | # Check https://docs.gpt4all.io/gpt4all_python.html for supported backends 120 | llm = GPT4All(model=local_path, backend='gptj', callbacks=callbacks, verbose=True) 121 | 122 | 123 | 124 | 125 | 126 | 127 | llm_chain = LLMChain(prompt=prompt, llm=llm) 128 | 129 | 130 | 131 | 132 | 133 | 134 | question = "What NFL team won the Super Bowl in the year Justin Bieber was born?" 135 | 136 | llm_chain.run(question) 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | previous 151 | GooseAI 152 | 153 | 154 | 155 | 156 | next 157 | Hugging Face Hub 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | Contents 168 | 169 | 170 | 171 | Specify Model 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | By Harrison Chase 182 | 183 | 184 | 185 | 186 | 187 | © Copyright 2023, Harrison Chase. 188 | 189 | 190 | 191 | 192 | 193 | Last updated on Jun 13, 2023. 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | -------------------------------------------------------------------------------- /data/langchain_doc_small/8_Getting_Started_Getting.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | Getting Started 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Getting Started# 25 | This notebook goes over how to use the LLM class in LangChain. 26 | The LLM class is a class designed for interfacing with LLMs. There are lots of LLM providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them. In this part of the documentation, we will focus on generic LLM functionality. For details on working with a specific LLM wrapper, please see the examples in the How-To section. 27 | For this notebook, we will work with an OpenAI LLM wrapper, although the functionalities highlighted are generic for all LLM types. 28 | 29 | 30 | from langchain.llms import OpenAI 31 | 32 | 33 | 34 | 35 | 36 | 37 | llm = OpenAI(model_name="text-ada-001", n=2, best_of=2) 38 | 39 | 40 | 41 | 42 | Generate Text: The most basic functionality an LLM has is just the ability to call it, passing in a string and getting back a string. 43 | 44 | 45 | llm("Tell me a joke") 46 | 47 | 48 | 49 | 50 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 51 | 52 | 53 | 54 | 55 | Generate: More broadly, you can call it with a list of inputs, getting back a more complete response than just the text. This complete response includes things like multiple top responses, as well as LLM provider specific information 56 | 57 | 58 | llm_result = llm.generate(["Tell me a joke", "Tell me a poem"]*15) 59 | 60 | 61 | 62 | 63 | 64 | 65 | len(llm_result.generations) 66 | 67 | 68 | 69 | 70 | 30 71 | 72 | 73 | 74 | 75 | 76 | 77 | llm_result.generations[0] 78 | 79 | 80 | 81 | 82 | [Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'), 83 | Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side.')] 84 | 85 | 86 | 87 | 88 | 89 | 90 | llm_result.generations[-1] 91 | 92 | 93 | 94 | 95 | [Generation(text="\n\nWhat if love neverspeech\n\nWhat if love never ended\n\nWhat if love was only a feeling\n\nI'll never know this love\n\nIt's not a feeling\n\nBut it's what we have for each other\n\nWe just know that love is something strong\n\nAnd we can't help but be happy\n\nWe just feel what love is for us\n\nAnd we love each other with all our heart\n\nWe just don't know how\n\nHow it will go\n\nBut we know that love is something strong\n\nAnd we'll always have each other\n\nIn our lives."), 96 | Generation(text='\n\nOnce upon a time\n\nThere was a love so pure and true\n\nIt lasted for centuries\n\nAnd never became stale or dry\n\nIt was moving and alive\n\nAnd the heart of the love-ick\n\nIs still beating strong and true.')] 97 | 98 | 99 | 100 | 101 | You can also access provider specific information that is returned. This information is NOT standardized across providers. 102 | 103 | 104 | llm_result.llm_output 105 | 106 | 107 | 108 | 109 | {'token_usage': {'completion_tokens': 3903, 110 | 'total_tokens': 4023, 111 | 'prompt_tokens': 120}} 112 | 113 | 114 | 115 | 116 | Number of Tokens: You can also estimate how many tokens a piece of text will be in that model. This is useful because models have a context length (and cost more for more tokens), which means you need to be aware of how long the text you are passing in is. 117 | Notice that by default the tokens are estimated using tiktoken (except for legacy version <3.8, where a Hugging Face tokenizer is used) 118 | 119 | 120 | llm.get_num_tokens("what a joke") 121 | 122 | 123 | 124 | 125 | 3 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | previous 139 | LLMs 140 | 141 | 142 | 143 | 144 | next 145 | Generic Functionality 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | By Harrison Chase 159 | 160 | 161 | 162 | 163 | 164 | © Copyright 2023, Harrison Chase. 165 | 166 | 167 | 168 | 169 | 170 | Last updated on Jun 14, 2023. 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /data/langchain_doc_small/37_Hugging_Face_Hub.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | Hugging Face Hub 21 | 22 | 23 | 24 | 25 | Contents 26 | 27 | 28 | 29 | Examples 30 | StableLM, by Stability AI 31 | Dolly, by Databricks 32 | Camel, by Writer 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Hugging Face Hub# 44 | The Hugging Face Hub is a platform with over 120k models, 20k datasets, and 50k demo apps (Spaces), all open source and publicly available, in an online platform where people can easily collaborate and build ML together. 45 | This example showcases how to connect to the Hugging Face Hub. 46 | To use, you should have the huggingface_hub python package installed. 47 | 48 | 49 | !pip install huggingface_hub > /dev/null 50 | 51 | 52 | 53 | 54 | 55 | 56 | # get a token: https://huggingface.co/docs/api-inference/quicktour#get-your-api-token 57 | 58 | from getpass import getpass 59 | 60 | HUGGINGFACEHUB_API_TOKEN = getpass() 61 | 62 | 63 | 64 | 65 | 66 | 67 | import os 68 | os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN 69 | 70 | 71 | 72 | 73 | Select a Model 74 | 75 | 76 | from langchain import HuggingFaceHub 77 | 78 | repo_id = "google/flan-t5-xl" # See https://huggingface.co/models?pipeline_tag=text-generation&sort=downloads for some other options 79 | 80 | llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0, "max_length":64}) 81 | 82 | 83 | 84 | 85 | 86 | 87 | from langchain import PromptTemplate, LLMChain 88 | 89 | template = """Question: {question} 90 | 91 | Answer: Let's think step by step.""" 92 | prompt = PromptTemplate(template=template, input_variables=["question"]) 93 | llm_chain = LLMChain(prompt=prompt, llm=llm) 94 | 95 | question = "Who won the FIFA World Cup in the year 1994? " 96 | 97 | print(llm_chain.run(question)) 98 | 99 | 100 | 101 | 102 | 103 | Examples# 104 | Below are some examples of models you can access through the Hugging Face Hub integration. 105 | 106 | StableLM, by Stability AI# 107 | See Stability AI’s organization page for a list of available models. 108 | 109 | 110 | repo_id = "stabilityai/stablelm-tuned-alpha-3b" 111 | # Others include stabilityai/stablelm-base-alpha-3b 112 | # as well as 7B parameter versions 113 | 114 | 115 | 116 | 117 | 118 | 119 | llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0, "max_length":64}) 120 | 121 | 122 | 123 | 124 | 125 | 126 | # Reuse the prompt and question from above. 127 | llm_chain = LLMChain(prompt=prompt, llm=llm) 128 | print(llm_chain.run(question)) 129 | 130 | 131 | 132 | 133 | 134 | 135 | Dolly, by Databricks# 136 | See Databricks organization page for a list of available models. 137 | 138 | 139 | from langchain import HuggingFaceHub 140 | 141 | repo_id = "databricks/dolly-v2-3b" 142 | 143 | llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0, "max_length":64}) 144 | 145 | 146 | 147 | 148 | 149 | 150 | # Reuse the prompt and question from above. 151 | llm_chain = LLMChain(prompt=prompt, llm=llm) 152 | print(llm_chain.run(question)) 153 | 154 | 155 | 156 | 157 | 158 | 159 | Camel, by Writer# 160 | See Writer’s organization page for a list of available models. 161 | 162 | 163 | from langchain import HuggingFaceHub 164 | 165 | repo_id = "Writer/camel-5b-hf" # See https://huggingface.co/Writer for other options 166 | llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0, "max_length":64}) 167 | 168 | 169 | 170 | 171 | 172 | 173 | # Reuse the prompt and question from above. 174 | llm_chain = LLMChain(prompt=prompt, llm=llm) 175 | print(llm_chain.run(question)) 176 | 177 | 178 | 179 | 180 | And many more! 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | previous 192 | GPT4All 193 | 194 | 195 | 196 | 197 | next 198 | Hugging Face Pipeline 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | Contents 209 | 210 | 211 | 212 | Examples 213 | StableLM, by Stability AI 214 | Dolly, by Databricks 215 | Camel, by Writer 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | By Harrison Chase 228 | 229 | 230 | 231 | 232 | 233 | © Copyright 2023, Harrison Chase. 234 | 235 | 236 | 237 | 238 | 239 | Last updated on Jun 13, 2023. 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | -------------------------------------------------------------------------------- /data/langchain_doc_small/16_How_to_stream.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | How to stream LLM and Chat Model responses 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | How to stream LLM and Chat Model responses# 25 | LangChain provides streaming support for LLMs. Currently, we support streaming for the OpenAI, ChatOpenAI, and ChatAnthropic implementations, but streaming support for other LLM implementations is on the roadmap. To utilize streaming, use a CallbackHandler that implements on_llm_new_token. In this example, we are using StreamingStdOutCallbackHandler. 26 | 27 | 28 | from langchain.llms import OpenAI 29 | from langchain.chat_models import ChatOpenAI, ChatAnthropic 30 | from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler 31 | from langchain.schema import HumanMessage 32 | 33 | 34 | 35 | 36 | 37 | 38 | llm = OpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0) 39 | resp = llm("Write me a song about sparkling water.") 40 | 41 | 42 | 43 | 44 | Verse 1 45 | I'm sippin' on sparkling water, 46 | It's so refreshing and light, 47 | It's the perfect way to quench my thirst 48 | On a hot summer night. 49 | 50 | Chorus 51 | Sparkling water, sparkling water, 52 | It's the best way to stay hydrated, 53 | It's so crisp and so clean, 54 | It's the perfect way to stay refreshed. 55 | 56 | Verse 2 57 | I'm sippin' on sparkling water, 58 | It's so bubbly and bright, 59 | It's the perfect way to cool me down 60 | On a hot summer night. 61 | 62 | Chorus 63 | Sparkling water, sparkling water, 64 | It's the best way to stay hydrated, 65 | It's so crisp and so clean, 66 | It's the perfect way to stay refreshed. 67 | 68 | Verse 3 69 | I'm sippin' on sparkling water, 70 | It's so light and so clear, 71 | It's the perfect way to keep me cool 72 | On a hot summer night. 73 | 74 | Chorus 75 | Sparkling water, sparkling water, 76 | It's the best way to stay hydrated, 77 | It's so crisp and so clean, 78 | It's the perfect way to stay refreshed. 79 | 80 | 81 | 82 | 83 | We still have access to the end LLMResult if using generate. However, token_usage is not currently supported for streaming. 84 | 85 | 86 | llm.generate(["Tell me a joke."]) 87 | 88 | 89 | 90 | 91 | Q: What did the fish say when it hit the wall? 92 | A: Dam! 93 | 94 | 95 | LLMResult(generations=[[Generation(text='\n\nQ: What did the fish say when it hit the wall?\nA: Dam!', generation_info={'finish_reason': 'stop', 'logprobs': None})]], llm_output={'token_usage': {}, 'model_name': 'text-davinci-003'}) 96 | 97 | 98 | 99 | 100 | Here’s an example with the ChatOpenAI chat model implementation: 101 | 102 | 103 | chat = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0) 104 | resp = chat([HumanMessage(content="Write me a song about sparkling water.")]) 105 | 106 | 107 | 108 | 109 | Verse 1: 110 | Bubbles rising to the top 111 | A refreshing drink that never stops 112 | Clear and crisp, it's oh so pure 113 | Sparkling water, I can't ignore 114 | 115 | Chorus: 116 | Sparkling water, oh how you shine 117 | A taste so clean, it's simply divine 118 | You quench my thirst, you make me feel alive 119 | Sparkling water, you're my favorite vibe 120 | 121 | Verse 2: 122 | No sugar, no calories, just H2O 123 | A drink that's good for me, don't you know 124 | With lemon or lime, you're even better 125 | Sparkling water, you're my forever 126 | 127 | Chorus: 128 | Sparkling water, oh how you shine 129 | A taste so clean, it's simply divine 130 | You quench my thirst, you make me feel alive 131 | Sparkling water, you're my favorite vibe 132 | 133 | Bridge: 134 | You're my go-to drink, day or night 135 | You make me feel so light 136 | I'll never give you up, you're my true love 137 | Sparkling water, you're sent from above 138 | 139 | Chorus: 140 | Sparkling water, oh how you shine 141 | A taste so clean, it's simply divine 142 | You quench my thirst, you make me feel alive 143 | Sparkling water, you're my favorite vibe 144 | 145 | Outro: 146 | Sparkling water, you're the one for me 147 | I'll never let you go, can't you see 148 | You're my drink of choice, forevermore 149 | Sparkling water, I adore. 150 | 151 | 152 | 153 | 154 | Here is an example with the ChatAnthropic chat model implementation, which uses their claude model. 155 | 156 | 157 | chat = ChatAnthropic(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0) 158 | resp = chat([HumanMessage(content="Write me a song about sparkling water.")]) 159 | 160 | 161 | 162 | 163 | Here is my attempt at a song about sparkling water: 164 | 165 | Sparkling water, bubbles so bright, 166 | Dancing in the glass with delight. 167 | Refreshing and crisp, a fizzy delight, 168 | Quenching my thirst with each sip I take. 169 | The carbonation tickles my tongue, 170 | As the refreshing water song is sung. 171 | Lime or lemon, a citrus twist, 172 | Makes sparkling water such a bliss. 173 | Healthy and hydrating, a drink so pure, 174 | Sparkling water, always alluring. 175 | Bubbles ascending in a stream, 176 | Sparkling water, you're my dream! 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | previous 190 | How to serialize LLM classes 191 | 192 | 193 | 194 | 195 | next 196 | How to track token usage 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | By Harrison Chase 210 | 211 | 212 | 213 | 214 | 215 | © Copyright 2023, Harrison Chase. 216 | 217 | 218 | 219 | 220 | 221 | Last updated on Jun 14, 2023. 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | -------------------------------------------------------------------------------- /data/langchain_doc_small/4_Tutorials_Contents_DeepLearning_AI.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Tutorials 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | DeepLearning.AI course 27 | Handbook 28 | Tutorials 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Tutorials# 44 | ⛓ icon marks a new addition [last update 2023-05-15] 45 | 46 | DeepLearning.AI course# 47 | ⛓LangChain for LLM Application Development by Harrison Chase presented by Andrew Ng 48 | 49 | 50 | Handbook# 51 | LangChain AI Handbook By James Briggs and Francisco Ingham 52 | 53 | 54 | Tutorials# 55 | LangChain Tutorials by Edrick: 56 | 57 | ⛓ LangChain, Chroma DB, OpenAI Beginner Guide | ChatGPT with your PDF 58 | ⛓ LangChain 101: The Complete Beginner’s Guide 59 | 60 | LangChain Crash Course: Build an AutoGPT app in 25 minutes by Nicholas Renotte 61 | LangChain Crash Course - Build apps with language models by Patrick Loeber 62 | LangChain Explained in 13 Minutes | QuickStart Tutorial for Beginners by Rabbitmetrics 63 | 64 | 65 | # 66 | LangChain for Gen AI and LLMs by James Briggs: 67 | 68 | #1 Getting Started with GPT-3 vs. Open Source LLMs 69 | #2 Prompt Templates for GPT 3.5 and other LLMs 70 | #3 LLM Chains using GPT 3.5 and other LLMs 71 | #4 Chatbot Memory for Chat-GPT, Davinci + other LLMs 72 | #5 Chat with OpenAI in LangChain 73 | ⛓ #6 Fixing LLM Hallucinations with Retrieval Augmentation in LangChain 74 | ⛓ #7 LangChain Agents Deep Dive with GPT 3.5 75 | ⛓ #8 Create Custom Tools for Chatbots in LangChain 76 | ⛓ #9 Build Conversational Agents with Vector DBs 77 | 78 | 79 | 80 | # 81 | LangChain 101 by Data Independent: 82 | 83 | What Is LangChain? - LangChain + ChatGPT Overview 84 | Quickstart Guide 85 | Beginner Guide To 7 Essential Concepts 86 | OpenAI + Wolfram Alpha 87 | Ask Questions On Your Custom (or Private) Files 88 | Connect Google Drive Files To OpenAI 89 | YouTube Transcripts + OpenAI 90 | Question A 300 Page Book (w/ OpenAI + Pinecone) 91 | Workaround OpenAI's Token Limit With Chain Types 92 | Build Your Own OpenAI + LangChain Web App in 23 Minutes 93 | Working With The New ChatGPT API 94 | OpenAI + LangChain Wrote Me 100 Custom Sales Emails 95 | Structured Output From OpenAI (Clean Dirty Data) 96 | Connect OpenAI To +5,000 Tools (LangChain + Zapier) 97 | Use LLMs To Extract Data From Text (Expert Mode) 98 | ⛓ Extract Insights From Interview Transcripts Using LLMs 99 | ⛓ 5 Levels Of LLM Summarizing: Novice to Expert 100 | 101 | 102 | 103 | # 104 | LangChain How to and guides by Sam Witteveen: 105 | 106 | LangChain Basics - LLMs & PromptTemplates with Colab 107 | LangChain Basics - Tools and Chains 108 | ChatGPT API Announcement & Code Walkthrough with LangChain 109 | Conversations with Memory (explanation & code walkthrough) 110 | Chat with Flan20B 111 | Using Hugging Face Models locally (code walkthrough) 112 | PAL : Program-aided Language Models with LangChain code 113 | Building a Summarization System with LangChain and GPT-3 - Part 1 114 | Building a Summarization System with LangChain and GPT-3 - Part 2 115 | Microsoft’s Visual ChatGPT using LangChain 116 | LangChain Agents - Joining Tools and Chains with Decisions 117 | Comparing LLMs with LangChain 118 | Using Constitutional AI in LangChain 119 | Talking to Alpaca with LangChain - Creating an Alpaca Chatbot 120 | Talk to your CSV & Excel with LangChain 121 | BabyAGI: Discover the Power of Task-Driven Autonomous Agents! 122 | Improve your BabyAGI with LangChain 123 | ⛓ Master PDF Chat with LangChain - Your essential guide to queries on documents 124 | ⛓ Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools 125 | ⛓ Building Custom Tools and Agents with LangChain (gpt-3.5-turbo) 126 | ⛓ LangChain Retrieval QA Over Multiple Files with ChromaDB 127 | ⛓ LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs 128 | ⛓ LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!! 129 | 130 | 131 | 132 | # 133 | LangChain by Prompt Engineering: 134 | 135 | LangChain Crash Course — All You Need to Know to Build Powerful Apps with LLMs 136 | Working with MULTIPLE PDF Files in LangChain: ChatGPT for your Data 137 | ChatGPT for YOUR OWN PDF files with LangChain 138 | Talk to YOUR DATA without OpenAI APIs: LangChain 139 | ⛓️ CHATGPT For WEBSITES: Custom ChatBOT 140 | 141 | 142 | 143 | # 144 | LangChain by Chat with data 145 | 146 | LangChain Beginner’s Tutorial for Typescript/Javascript 147 | GPT-4 Tutorial: How to Chat With Multiple PDF Files (~1000 pages of Tesla’s 10-K Annual Reports) 148 | GPT-4 & LangChain Tutorial: How to Chat With A 56-Page PDF Document (w/Pinecone) 149 | ⛓ LangChain & Supabase Tutorial: How to Build a ChatGPT Chatbot For Your Website 150 | 151 | 152 | 153 | # 154 | Get SH*T Done with Prompt Engineering and LangChain by Venelin Valkov 155 | 156 | Getting Started with LangChain: Load Custom Data, Run OpenAI Models, Embeddings and ChatGPT 157 | Loaders, Indexes & Vectorstores in LangChain: Question Answering on PDF files with ChatGPT 158 | LangChain Models: ChatGPT, Flan Alpaca, OpenAI Embeddings, Prompt Templates & Streaming 159 | LangChain Chains: Use ChatGPT to Build Conversational Agents, Summaries and Q&A on Text With LLMs 160 | Analyze Custom CSV Data with GPT-4 using Langchain 161 | ⛓ Build ChatGPT Chatbots with LangChain Memory: Understanding and Implementing Memory in Conversations 162 | 163 | 164 | ⛓ icon marks a new addition [last update 2023-05-15] 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | previous 175 | Concepts 176 | 177 | 178 | 179 | 180 | next 181 | Models 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | Contents 192 | 193 | 194 | 195 | DeepLearning.AI course 196 | Handbook 197 | Tutorials 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | By Harrison Chase 214 | 215 | 216 | 217 | 218 | 219 | © Copyright 2023, Harrison Chase. 220 | 221 | 222 | 223 | 224 | 225 | Last updated on Jun 14, 2023. 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | -------------------------------------------------------------------------------- /data/langchain_doc_small/0_Welcome_to_LangChain.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Welcome to LangChain 17 | 18 | 19 | 20 | 21 | Contents 22 | 23 | 24 | 25 | Getting Started 26 | Modules 27 | Use Cases 28 | Reference Docs 29 | Ecosystem 30 | Additional Resources 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Welcome to LangChain# 40 | 41 | LangChain is a framework for developing applications powered by language models. We believe that the most powerful and differentiated applications will not only call out to a language model, but will also be: 42 | 43 | 44 | Data-aware: connect a language model to other sources of data 45 | Agentic: allow a language model to interact with its environment 46 | 47 | 48 | The LangChain framework is designed around these principles. 49 | 50 | 51 | This is the Python specific portion of the documentation. For a purely conceptual guide to LangChain, see here. For the JavaScript documentation, see here. 52 | 53 | 54 | Getting Started# 55 | 56 | How to get started using LangChain to create an Language Model application. 57 | 58 | 59 | Quickstart Guide 60 | 61 | 62 | Concepts and terminology. 63 | 64 | 65 | Concepts and terminology 66 | 67 | 68 | Tutorials created by community experts and presented on YouTube. 69 | 70 | 71 | Tutorials 72 | 73 | 74 | 75 | 76 | 77 | Modules# 78 | 79 | These modules are the core abstractions which we view as the building blocks of any LLM-powered application. 80 | 81 | For each module LangChain provides standard, extendable interfaces. LangChain also provides external integrations and even end-to-end implementations for off-the-shelf use. 82 | 83 | The docs for each module contain quickstart examples, how-to guides, reference docs, and conceptual guides. 84 | 85 | 86 | The modules are (from least to most complex): 87 | 88 | 89 | Models: Supported model types and integrations. 90 | Prompts: Prompt management, optimization, and serialization. 91 | Memory: Memory refers to state that is persisted between calls of a chain/agent. 92 | Indexes: Language models become much more powerful when combined with application-specific data - this module contains interfaces and integrations for loading, querying and updating external data. 93 | Chains: Chains are structured sequences of calls (to an LLM or to a different utility). 94 | Agents: An agent is a Chain in which an LLM, given a high-level directive and a set of tools, repeatedly decides an action, executes the action and observes the outcome until the high-level directive is complete. 95 | Callbacks: Callbacks let you log and stream the intermediate steps of any chain, making it easy to observe, debug, and evaluate the internals of an application. 96 | 97 | 98 | 99 | 100 | 101 | Use Cases# 102 | 103 | Best practices and built-in implementations for common LangChain use cases: 104 | 105 | 106 | Autonomous Agents: Autonomous agents are long-running agents that take many steps in an attempt to accomplish an objective. Examples include AutoGPT and BabyAGI. 107 | Agent Simulations: Putting agents in a sandbox and observing how they interact with each other and react to events can be an effective way to evaluate their long-range reasoning and planning abilities. 108 | Personal Assistants: One of the primary LangChain use cases. Personal assistants need to take actions, remember interactions, and have knowledge about your data. 109 | Question Answering: Another common LangChain use case. Answering questions over specific documents, only utilizing the information in those documents to construct an answer. 110 | Chatbots: Language models love to chat, making this a very natural use of them. 111 | Querying Tabular Data: Recommended reading if you want to use language models to query structured data (CSVs, SQL, dataframes, etc). 112 | Code Understanding: Recommended reading if you want to use language models to analyze code. 113 | Interacting with APIs: Enabling language models to interact with APIs is extremely powerful. It gives them access to up-to-date information and allows them to take actions. 114 | Extraction: Extract structured information from text. 115 | Summarization: Compressing longer documents. A type of Data-Augmented Generation. 116 | Evaluation: Generative models are hard to evaluate with traditional metrics. One promising approach is to use language models themselves to do the evaluation. 117 | 118 | 119 | 120 | 121 | 122 | Reference Docs# 123 | 124 | Full documentation on all methods, classes, installation methods, and integration setups for LangChain. 125 | 126 | 127 | LangChain Installation 128 | Reference Documentation 129 | 130 | 131 | 132 | 133 | 134 | Ecosystem# 135 | 136 | LangChain integrates a lot of different LLMs, systems, and products. 137 | From the other side, many systems and products depend on LangChain. 138 | It creates a vibrant and thriving ecosystem. 139 | 140 | 141 | Integrations: Guides for how other products can be used with LangChain. 142 | Dependents: List of repositories that use LangChain. 143 | Deployments: A collection of instructions, code snippets, and template repositories for deploying LangChain apps. 144 | 145 | 146 | 147 | 148 | 149 | Additional Resources# 150 | 151 | Additional resources we think may be useful as you develop your application! 152 | 153 | 154 | LangChainHub: The LangChainHub is a place to share and explore other prompts, chains, and agents. 155 | Gallery: A collection of great projects that use Langchain, compiled by the folks at Kyrolabs. Useful for finding inspiration and example implementations. 156 | Deploying LLMs in Production: A collection of best practices and tutorials for deploying LLMs in production. 157 | Tracing: A guide on using tracing in LangChain to visualize the execution of chains and agents. 158 | Model Laboratory: Experimenting with different prompts, models, and chains is a big part of developing the best possible application. The ModelLaboratory makes it easy to do so. 159 | Discord: Join us on our Discord to discuss all things LangChain! 160 | YouTube: A collection of the LangChain tutorials and videos. 161 | Production Support: As you move your LangChains into production, we’d love to offer more comprehensive support. Please fill out this form and we’ll set up a dedicated support Slack channel. 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | next 174 | Quickstart Guide 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | Contents 185 | 186 | 187 | 188 | Getting Started 189 | Modules 190 | Use Cases 191 | Reference Docs 192 | Ecosystem 193 | Additional Resources 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | By Harrison Chase 204 | 205 | 206 | 207 | 208 | 209 | © Copyright 2023, Harrison Chase. 210 | 211 | 212 | 213 | 214 | 215 | Last updated on Jun 14, 2023. 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /data/langchain_doc_small/1_Welcome_to_LangChain.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Welcome to LangChain 17 | 18 | 19 | 20 | 21 | Contents 22 | 23 | 24 | 25 | Getting Started 26 | Modules 27 | Use Cases 28 | Reference Docs 29 | Ecosystem 30 | Additional Resources 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Welcome to LangChain# 40 | 41 | LangChain is a framework for developing applications powered by language models. We believe that the most powerful and differentiated applications will not only call out to a language model, but will also be: 42 | 43 | 44 | Data-aware: connect a language model to other sources of data 45 | Agentic: allow a language model to interact with its environment 46 | 47 | 48 | The LangChain framework is designed around these principles. 49 | 50 | 51 | This is the Python specific portion of the documentation. For a purely conceptual guide to LangChain, see here. For the JavaScript documentation, see here. 52 | 53 | 54 | Getting Started# 55 | 56 | How to get started using LangChain to create an Language Model application. 57 | 58 | 59 | Quickstart Guide 60 | 61 | 62 | Concepts and terminology. 63 | 64 | 65 | Concepts and terminology 66 | 67 | 68 | Tutorials created by community experts and presented on YouTube. 69 | 70 | 71 | Tutorials 72 | 73 | 74 | 75 | 76 | 77 | Modules# 78 | 79 | These modules are the core abstractions which we view as the building blocks of any LLM-powered application. 80 | 81 | For each module LangChain provides standard, extendable interfaces. LangChain also provides external integrations and even end-to-end implementations for off-the-shelf use. 82 | 83 | The docs for each module contain quickstart examples, how-to guides, reference docs, and conceptual guides. 84 | 85 | 86 | The modules are (from least to most complex): 87 | 88 | 89 | Models: Supported model types and integrations. 90 | Prompts: Prompt management, optimization, and serialization. 91 | Memory: Memory refers to state that is persisted between calls of a chain/agent. 92 | Indexes: Language models become much more powerful when combined with application-specific data - this module contains interfaces and integrations for loading, querying and updating external data. 93 | Chains: Chains are structured sequences of calls (to an LLM or to a different utility). 94 | Agents: An agent is a Chain in which an LLM, given a high-level directive and a set of tools, repeatedly decides an action, executes the action and observes the outcome until the high-level directive is complete. 95 | Callbacks: Callbacks let you log and stream the intermediate steps of any chain, making it easy to observe, debug, and evaluate the internals of an application. 96 | 97 | 98 | 99 | 100 | 101 | Use Cases# 102 | 103 | Best practices and built-in implementations for common LangChain use cases: 104 | 105 | 106 | Autonomous Agents: Autonomous agents are long-running agents that take many steps in an attempt to accomplish an objective. Examples include AutoGPT and BabyAGI. 107 | Agent Simulations: Putting agents in a sandbox and observing how they interact with each other and react to events can be an effective way to evaluate their long-range reasoning and planning abilities. 108 | Personal Assistants: One of the primary LangChain use cases. Personal assistants need to take actions, remember interactions, and have knowledge about your data. 109 | Question Answering: Another common LangChain use case. Answering questions over specific documents, only utilizing the information in those documents to construct an answer. 110 | Chatbots: Language models love to chat, making this a very natural use of them. 111 | Querying Tabular Data: Recommended reading if you want to use language models to query structured data (CSVs, SQL, dataframes, etc). 112 | Code Understanding: Recommended reading if you want to use language models to analyze code. 113 | Interacting with APIs: Enabling language models to interact with APIs is extremely powerful. It gives them access to up-to-date information and allows them to take actions. 114 | Extraction: Extract structured information from text. 115 | Summarization: Compressing longer documents. A type of Data-Augmented Generation. 116 | Evaluation: Generative models are hard to evaluate with traditional metrics. One promising approach is to use language models themselves to do the evaluation. 117 | 118 | 119 | 120 | 121 | 122 | Reference Docs# 123 | 124 | Full documentation on all methods, classes, installation methods, and integration setups for LangChain. 125 | 126 | 127 | LangChain Installation 128 | Reference Documentation 129 | 130 | 131 | 132 | 133 | 134 | Ecosystem# 135 | 136 | LangChain integrates a lot of different LLMs, systems, and products. 137 | From the other side, many systems and products depend on LangChain. 138 | It creates a vibrant and thriving ecosystem. 139 | 140 | 141 | Integrations: Guides for how other products can be used with LangChain. 142 | Dependents: List of repositories that use LangChain. 143 | Deployments: A collection of instructions, code snippets, and template repositories for deploying LangChain apps. 144 | 145 | 146 | 147 | 148 | 149 | Additional Resources# 150 | 151 | Additional resources we think may be useful as you develop your application! 152 | 153 | 154 | LangChainHub: The LangChainHub is a place to share and explore other prompts, chains, and agents. 155 | Gallery: A collection of great projects that use Langchain, compiled by the folks at Kyrolabs. Useful for finding inspiration and example implementations. 156 | Deploying LLMs in Production: A collection of best practices and tutorials for deploying LLMs in production. 157 | Tracing: A guide on using tracing in LangChain to visualize the execution of chains and agents. 158 | Model Laboratory: Experimenting with different prompts, models, and chains is a big part of developing the best possible application. The ModelLaboratory makes it easy to do so. 159 | Discord: Join us on our Discord to discuss all things LangChain! 160 | YouTube: A collection of the LangChain tutorials and videos. 161 | Production Support: As you move your LangChains into production, we’d love to offer more comprehensive support. Please fill out this form and we’ll set up a dedicated support Slack channel. 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | next 174 | Quickstart Guide 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | Contents 185 | 186 | 187 | 188 | Getting Started 189 | Modules 190 | Use Cases 191 | Reference Docs 192 | Ecosystem 193 | Additional Resources 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | By Harrison Chase 204 | 205 | 206 | 207 | 208 | 209 | © Copyright 2023, Harrison Chase. 210 | 211 | 212 | 213 | 214 | 215 | Last updated on Jun 14, 2023. 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /data/langchain_doc_small/31_Databricks_Contents_Wrapping.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Databricks 20 | 21 | 22 | 23 | 24 | Contents 25 | 26 | 27 | 28 | Wrapping a serving endpoint 29 | Wrapping a cluster driver proxy app 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Databricks# 39 | The Databricks Lakehouse Platform unifies data, analytics, and AI on one platform. 40 | This example notebook shows how to wrap Databricks endpoints as LLMs in LangChain. 41 | It supports two endpoint types: 42 | 43 | Serving endpoint, recommended for production and development, 44 | Cluster driver proxy app, recommended for iteractive development. 45 | 46 | 47 | 48 | from langchain.llms import Databricks 49 | 50 | 51 | 52 | 53 | 54 | Wrapping a serving endpoint# 55 | Prerequisites: 56 | 57 | An LLM was registered and deployed to a Databricks serving endpoint. 58 | You have “Can Query” permission to the endpoint. 59 | 60 | The expected MLflow model signature is: 61 | 62 | inputs: [{"name": "prompt", "type": "string"}, {"name": "stop", "type": "list[string]"}] 63 | outputs: [{"type": "string"}] 64 | 65 | If the model signature is incompatible or you want to insert extra configs, you can set transform_input_fn and transform_output_fn accordingly. 66 | 67 | 68 | # If running a Databricks notebook attached to an interactive cluster in "single user" 69 | # or "no isolation shared" mode, you only need to specify the endpoint name to create 70 | # a `Databricks` instance to query a serving endpoint in the same workspace. 71 | llm = Databricks(endpoint_name="dolly") 72 | 73 | llm("How are you?") 74 | 75 | 76 | 77 | 78 | 'I am happy to hear that you are in good health and as always, you are appreciated.' 79 | 80 | 81 | 82 | 83 | 84 | 85 | llm("How are you?", stop=["."]) 86 | 87 | 88 | 89 | 90 | 'Good' 91 | 92 | 93 | 94 | 95 | 96 | 97 | # Otherwise, you can manually specify the Databricks workspace hostname and personal access token 98 | # or set `DATABRICKS_HOST` and `DATABRICKS_TOKEN` environment variables, respectively. 99 | # See https://docs.databricks.com/dev-tools/auth.html#databricks-personal-access-tokens 100 | # We strongly recommend not exposing the API token explicitly inside a notebook. 101 | # You can use Databricks secret manager to store your API token securely. 102 | # See https://docs.databricks.com/dev-tools/databricks-utils.html#secrets-utility-dbutilssecrets 103 | 104 | import os 105 | os.environ["DATABRICKS_TOKEN"] = dbutils.secrets.get("myworkspace", "api_token") 106 | 107 | llm = Databricks(host="myworkspace.cloud.databricks.com", endpoint_name="dolly") 108 | 109 | llm("How are you?") 110 | 111 | 112 | 113 | 114 | 'I am fine. Thank you!' 115 | 116 | 117 | 118 | 119 | 120 | 121 | # If the serving endpoint accepts extra parameters like `temperature`, 122 | # you can set them in `model_kwargs`. 123 | llm = Databricks(endpoint_name="dolly", model_kwargs={"temperature": 0.1}) 124 | 125 | llm("How are you?") 126 | 127 | 128 | 129 | 130 | 'I am fine.' 131 | 132 | 133 | 134 | 135 | 136 | 137 | # Use `transform_input_fn` and `transform_output_fn` if the serving endpoint 138 | # expects a different input schema and does not return a JSON string, 139 | # respectively, or you want to apply a prompt template on top. 140 | 141 | def transform_input(**request): 142 | full_prompt = f"""{request["prompt"]} 143 | Be Concise. 144 | """ 145 | request["prompt"] = full_prompt 146 | return request 147 | 148 | llm = Databricks(endpoint_name="dolly", transform_input_fn=transform_input) 149 | 150 | llm("How are you?") 151 | 152 | 153 | 154 | 155 | 'I’m Excellent. You?' 156 | 157 | 158 | 159 | 160 | 161 | 162 | Wrapping a cluster driver proxy app# 163 | Prerequisites: 164 | 165 | An LLM loaded on a Databricks interactive cluster in “single user” or “no isolation shared” mode. 166 | A local HTTP server running on the driver node to serve the model at "/" using HTTP POST with JSON input/output. 167 | It uses a port number between [3000, 8000] and listens to the driver IP address or simply 0.0.0.0 instead of localhost only. 168 | You have “Can Attach To” permission to the cluster. 169 | 170 | The expected server schema (using JSON schema) is: 171 | 172 | inputs: 173 | {"type": "object", 174 | "properties": { 175 | "prompt": {"type": "string"}, 176 | "stop": {"type": "array", "items": {"type": "string"}}}, 177 | "required": ["prompt"]} 178 | 179 | 180 | 181 | outputs: {"type": "string"} 182 | 183 | If the server schema is incompatible or you want to insert extra configs, you can use transform_input_fn and transform_output_fn accordingly. 184 | The following is a minimal example for running a driver proxy app to serve an LLM: 185 | from flask import Flask, request, jsonify 186 | import torch 187 | from transformers import pipeline, AutoTokenizer, StoppingCriteria 188 | 189 | model = "databricks/dolly-v2-3b" 190 | tokenizer = AutoTokenizer.from_pretrained(model, padding_side="left") 191 | dolly = pipeline(model=model, tokenizer=tokenizer, trust_remote_code=True, device_map="auto") 192 | device = dolly.device 193 | 194 | class CheckStop(StoppingCriteria): 195 | def __init__(self, stop=None): 196 | super().__init__() 197 | self.stop = stop or [] 198 | self.matched = "" 199 | self.stop_ids = [tokenizer.encode(s, return_tensors='pt').to(device) for s in self.stop] 200 | def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs): 201 | for i, s in enumerate(self.stop_ids): 202 | if torch.all((s == input_ids[0][-s.shape[1]:])).item(): 203 | self.matched = self.stop[i] 204 | return True 205 | return False 206 | 207 | def llm(prompt, stop=None, **kwargs): 208 | check_stop = CheckStop(stop) 209 | result = dolly(prompt, stopping_criteria=[check_stop], **kwargs) 210 | return result[0]["generated_text"].rstrip(check_stop.matched) 211 | 212 | app = Flask("dolly") 213 | 214 | @app.route('/', methods=['POST']) 215 | def serve_llm(): 216 | resp = llm(**request.json) 217 | return jsonify(resp) 218 | 219 | app.run(host="0.0.0.0", port="7777") 220 | 221 | 222 | Once the server is running, you can create a Databricks instance to wrap it as an LLM. 223 | 224 | 225 | # If running a Databricks notebook attached to the same cluster that runs the app, 226 | # you only need to specify the driver port to create a `Databricks` instance. 227 | llm = Databricks(cluster_driver_port="7777") 228 | 229 | llm("How are you?") 230 | 231 | 232 | 233 | 234 | 'Hello, thank you for asking. It is wonderful to hear that you are well.' 235 | 236 | 237 | 238 | 239 | 240 | 241 | # Otherwise, you can manually specify the cluster ID to use, 242 | # as well as Databricks workspace hostname and personal access token. 243 | 244 | llm = Databricks(cluster_id="0000-000000-xxxxxxxx", cluster_driver_port="7777") 245 | 246 | llm("How are you?") 247 | 248 | 249 | 250 | 251 | 'I am well. You?' 252 | 253 | 254 | 255 | 256 | 257 | 258 | # If the app accepts extra parameters like `temperature`, 259 | # you can set them in `model_kwargs`. 260 | llm = Databricks(cluster_driver_port="7777", model_kwargs={"temperature": 0.1}) 261 | 262 | llm("How are you?") 263 | 264 | 265 | 266 | 267 | 'I am very well. It is a pleasure to meet you.' 268 | 269 | 270 | 271 | 272 | 273 | 274 | # Use `transform_input_fn` and `transform_output_fn` if the app 275 | # expects a different input schema and does not return a JSON string, 276 | # respectively, or you want to apply a prompt template on top. 277 | 278 | def transform_input(**request): 279 | full_prompt = f"""{request["prompt"]} 280 | Be Concise. 281 | """ 282 | request["prompt"] = full_prompt 283 | return request 284 | 285 | def transform_output(response): 286 | return response.upper() 287 | 288 | llm = Databricks( 289 | cluster_driver_port="7777", 290 | transform_input_fn=transform_input, 291 | transform_output_fn=transform_output) 292 | 293 | llm("How are you?") 294 | 295 | 296 | 297 | 298 | 'I AM DOING GREAT THANK YOU.' 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | previous 313 | C Transformers 314 | 315 | 316 | 317 | 318 | next 319 | DeepInfra 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | Contents 330 | 331 | 332 | 333 | Wrapping a serving endpoint 334 | Wrapping a cluster driver proxy app 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | By Harrison Chase 345 | 346 | 347 | 348 | 349 | 350 | © Copyright 2023, Harrison Chase. 351 | 352 | 353 | 354 | 355 | 356 | Last updated on Jun 13, 2023. 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | -------------------------------------------------------------------------------- /data/langchain_doc_small/13_How_and_why_.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | How (and why) to use the human input LLM 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | How (and why) to use the human input LLM# 25 | Similar to the fake LLM, LangChain provides a pseudo LLM class that can be used for testing, debugging, or educational purposes. This allows you to mock out calls to the LLM and simulate how a human would respond if they received the prompts. 26 | In this notebook, we go over how to use this. 27 | We start this with using the HumanInputLLM in an agent. 28 | 29 | 30 | from langchain.llms.human import HumanInputLLM 31 | 32 | 33 | 34 | 35 | 36 | 37 | from langchain.agents import load_tools 38 | from langchain.agents import initialize_agent 39 | from langchain.agents import AgentType 40 | 41 | 42 | 43 | 44 | Since we will use the WikipediaQueryRun tool in this notebook, you might need to install the wikipedia package if you haven’t done so already. 45 | 46 | 47 | %pip install wikipedia 48 | 49 | 50 | 51 | 52 | 53 | 54 | tools = load_tools(["wikipedia"]) 55 | llm = HumanInputLLM(prompt_func=lambda prompt: print(f"\n===PROMPT====\n{prompt}\n=====END OF PROMPT======")) 56 | 57 | 58 | 59 | 60 | 61 | 62 | agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 63 | 64 | 65 | 66 | 67 | 68 | 69 | agent.run("What is 'Bocchi the Rock!'?") 70 | 71 | 72 | 73 | 74 | > Entering new AgentExecutor chain... 75 | 76 | ===PROMPT==== 77 | Answer the following questions as best you can. You have access to the following tools: 78 | 79 | Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query. 80 | 81 | Use the following format: 82 | 83 | Question: the input question you must answer 84 | Thought: you should always think about what to do 85 | Action: the action to take, should be one of [Wikipedia] 86 | Action Input: the input to the action 87 | Observation: the result of the action 88 | ... (this Thought/Action/Action Input/Observation can repeat N times) 89 | Thought: I now know the final answer 90 | Final Answer: the final answer to the original input question 91 | 92 | Begin! 93 | 94 | Question: What is 'Bocchi the Rock!'? 95 | Thought: 96 | =====END OF PROMPT====== 97 | I need to use a tool. 98 | Action: Wikipedia 99 | Action Input: Bocchi the Rock!, Japanese four-panel manga and anime series. 100 | Observation: Page: Bocchi the Rock! 101 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 102 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 103 | 104 | Page: Manga Time Kirara 105 | Summary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia. 106 | 107 | Page: Manga Time Kirara Max 108 | Summary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the "Kirara" series, after "Manga Time Kirara" and "Manga Time Kirara Carat". The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month. 109 | Thought: 110 | ===PROMPT==== 111 | Answer the following questions as best you can. You have access to the following tools: 112 | 113 | Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query. 114 | 115 | Use the following format: 116 | 117 | Question: the input question you must answer 118 | Thought: you should always think about what to do 119 | Action: the action to take, should be one of [Wikipedia] 120 | Action Input: the input to the action 121 | Observation: the result of the action 122 | ... (this Thought/Action/Action Input/Observation can repeat N times) 123 | Thought: I now know the final answer 124 | Final Answer: the final answer to the original input question 125 | 126 | Begin! 127 | 128 | Question: What is 'Bocchi the Rock!'? 129 | Thought:I need to use a tool. 130 | Action: Wikipedia 131 | Action Input: Bocchi the Rock!, Japanese four-panel manga and anime series. 132 | Observation: Page: Bocchi the Rock! 133 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 134 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 135 | 136 | Page: Manga Time Kirara 137 | Summary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia. 138 | 139 | Page: Manga Time Kirara Max 140 | Summary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the "Kirara" series, after "Manga Time Kirara" and "Manga Time Kirara Carat". The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month. 141 | Thought: 142 | =====END OF PROMPT====== 143 | These are not relevant articles. 144 | Action: Wikipedia 145 | Action Input: Bocchi the Rock!, Japanese four-panel manga series written and illustrated by Aki Hamaji. 146 | Observation: Page: Bocchi the Rock! 147 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 148 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 149 | Thought: 150 | ===PROMPT==== 151 | Answer the following questions as best you can. You have access to the following tools: 152 | 153 | Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query. 154 | 155 | Use the following format: 156 | 157 | Question: the input question you must answer 158 | Thought: you should always think about what to do 159 | Action: the action to take, should be one of [Wikipedia] 160 | Action Input: the input to the action 161 | Observation: the result of the action 162 | ... (this Thought/Action/Action Input/Observation can repeat N times) 163 | Thought: I now know the final answer 164 | Final Answer: the final answer to the original input question 165 | 166 | Begin! 167 | 168 | Question: What is 'Bocchi the Rock!'? 169 | Thought:I need to use a tool. 170 | Action: Wikipedia 171 | Action Input: Bocchi the Rock!, Japanese four-panel manga and anime series. 172 | Observation: Page: Bocchi the Rock! 173 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 174 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 175 | 176 | Page: Manga Time Kirara 177 | Summary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia. 178 | 179 | Page: Manga Time Kirara Max 180 | Summary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the "Kirara" series, after "Manga Time Kirara" and "Manga Time Kirara Carat". The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month. 181 | Thought:These are not relevant articles. 182 | Action: Wikipedia 183 | Action Input: Bocchi the Rock!, Japanese four-panel manga series written and illustrated by Aki Hamaji. 184 | Observation: Page: Bocchi the Rock! 185 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 186 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 187 | Thought: 188 | =====END OF PROMPT====== 189 | It worked. 190 | Final Answer: Bocchi the Rock! is a four-panel manga series and anime television series. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 191 | 192 | > Finished chain. 193 | 194 | 195 | "Bocchi the Rock! is a four-panel manga series and anime television series. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim." 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | previous 209 | How (and why) to use the fake LLM 210 | 211 | 212 | 213 | 214 | next 215 | How to cache LLM calls 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | By Harrison Chase 229 | 230 | 231 | 232 | 233 | 234 | © Copyright 2023, Harrison Chase. 235 | 236 | 237 | 238 | 239 | 240 | Last updated on Jun 14, 2023. 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | -------------------------------------------------------------------------------- /data/langchain_doc_small/13_How_(and_why).txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | How (and why) to use the human input LLM 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | How (and why) to use the human input LLM# 28 | Similar to the fake LLM, LangChain provides a pseudo LLM class that can be used for testing, debugging, or educational purposes. This allows you to mock out calls to the LLM and simulate how a human would respond if they received the prompts. 29 | In this notebook, we go over how to use this. 30 | We start this with using the HumanInputLLM in an agent. 31 | 32 | 33 | from langchain.llms.human import HumanInputLLM 34 | 35 | 36 | 37 | 38 | 39 | 40 | from langchain.agents import load_tools 41 | from langchain.agents import initialize_agent 42 | from langchain.agents import AgentType 43 | 44 | 45 | 46 | 47 | Since we will use the WikipediaQueryRun tool in this notebook, you might need to install the wikipedia package if you haven’t done so already. 48 | 49 | 50 | %pip install wikipedia 51 | 52 | 53 | 54 | 55 | 56 | 57 | tools = load_tools(["wikipedia"]) 58 | llm = HumanInputLLM(prompt_func=lambda prompt: print(f"\n===PROMPT====\n{prompt}\n=====END OF PROMPT======")) 59 | 60 | 61 | 62 | 63 | 64 | 65 | agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 66 | 67 | 68 | 69 | 70 | 71 | 72 | agent.run("What is 'Bocchi the Rock!'?") 73 | 74 | 75 | 76 | 77 | > Entering new AgentExecutor chain... 78 | 79 | ===PROMPT==== 80 | Answer the following questions as best you can. You have access to the following tools: 81 | 82 | Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query. 83 | 84 | Use the following format: 85 | 86 | Question: the input question you must answer 87 | Thought: you should always think about what to do 88 | Action: the action to take, should be one of [Wikipedia] 89 | Action Input: the input to the action 90 | Observation: the result of the action 91 | ... (this Thought/Action/Action Input/Observation can repeat N times) 92 | Thought: I now know the final answer 93 | Final Answer: the final answer to the original input question 94 | 95 | Begin! 96 | 97 | Question: What is 'Bocchi the Rock!'? 98 | Thought: 99 | =====END OF PROMPT====== 100 | I need to use a tool. 101 | Action: Wikipedia 102 | Action Input: Bocchi the Rock!, Japanese four-panel manga and anime series. 103 | Observation: Page: Bocchi the Rock! 104 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 105 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 106 | 107 | Page: Manga Time Kirara 108 | Summary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia. 109 | 110 | Page: Manga Time Kirara Max 111 | Summary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the "Kirara" series, after "Manga Time Kirara" and "Manga Time Kirara Carat". The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month. 112 | Thought: 113 | ===PROMPT==== 114 | Answer the following questions as best you can. You have access to the following tools: 115 | 116 | Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query. 117 | 118 | Use the following format: 119 | 120 | Question: the input question you must answer 121 | Thought: you should always think about what to do 122 | Action: the action to take, should be one of [Wikipedia] 123 | Action Input: the input to the action 124 | Observation: the result of the action 125 | ... (this Thought/Action/Action Input/Observation can repeat N times) 126 | Thought: I now know the final answer 127 | Final Answer: the final answer to the original input question 128 | 129 | Begin! 130 | 131 | Question: What is 'Bocchi the Rock!'? 132 | Thought:I need to use a tool. 133 | Action: Wikipedia 134 | Action Input: Bocchi the Rock!, Japanese four-panel manga and anime series. 135 | Observation: Page: Bocchi the Rock! 136 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 137 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 138 | 139 | Page: Manga Time Kirara 140 | Summary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia. 141 | 142 | Page: Manga Time Kirara Max 143 | Summary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the "Kirara" series, after "Manga Time Kirara" and "Manga Time Kirara Carat". The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month. 144 | Thought: 145 | =====END OF PROMPT====== 146 | These are not relevant articles. 147 | Action: Wikipedia 148 | Action Input: Bocchi the Rock!, Japanese four-panel manga series written and illustrated by Aki Hamaji. 149 | Observation: Page: Bocchi the Rock! 150 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 151 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 152 | Thought: 153 | ===PROMPT==== 154 | Answer the following questions as best you can. You have access to the following tools: 155 | 156 | Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query. 157 | 158 | Use the following format: 159 | 160 | Question: the input question you must answer 161 | Thought: you should always think about what to do 162 | Action: the action to take, should be one of [Wikipedia] 163 | Action Input: the input to the action 164 | Observation: the result of the action 165 | ... (this Thought/Action/Action Input/Observation can repeat N times) 166 | Thought: I now know the final answer 167 | Final Answer: the final answer to the original input question 168 | 169 | Begin! 170 | 171 | Question: What is 'Bocchi the Rock!'? 172 | Thought:I need to use a tool. 173 | Action: Wikipedia 174 | Action Input: Bocchi the Rock!, Japanese four-panel manga and anime series. 175 | Observation: Page: Bocchi the Rock! 176 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 177 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 178 | 179 | Page: Manga Time Kirara 180 | Summary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia. 181 | 182 | Page: Manga Time Kirara Max 183 | Summary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the "Kirara" series, after "Manga Time Kirara" and "Manga Time Kirara Carat". The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month. 184 | Thought:These are not relevant articles. 185 | Action: Wikipedia 186 | Action Input: Bocchi the Rock!, Japanese four-panel manga series written and illustrated by Aki Hamaji. 187 | Observation: Page: Bocchi the Rock! 188 | Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunsha's seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022. 189 | An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 190 | Thought: 191 | =====END OF PROMPT====== 192 | It worked. 193 | Final Answer: Bocchi the Rock! is a four-panel manga series and anime television series. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim. 194 | 195 | > Finished chain. 196 | 197 | 198 | "Bocchi the Rock! is a four-panel manga series and anime television series. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the anime's visual creativity receiving acclaim." 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | previous 212 | How (and why) to use the fake LLM 213 | 214 | 215 | 216 | 217 | next 218 | How to cache LLM calls 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | By Harrison Chase 232 | 233 | 234 | 235 | 236 | 237 | © Copyright 2023, Harrison Chase. 238 | 239 | 240 | 241 | 242 | 243 | Last updated on Jun 13, 2023. 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | -------------------------------------------------------------------------------- /data/langchain_doc_small/14_How_to_cache.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | How to cache LLM calls 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | In Memory Cache 27 | SQLite Cache 28 | Redis Cache 29 | Standard Cache 30 | Semantic Cache 31 | 32 | 33 | GPTCache 34 | Momento Cache 35 | SQLAlchemy Cache 36 | Custom SQLAlchemy Schemas 37 | 38 | 39 | Optional Caching 40 | Optional Caching in Chains 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | How to cache LLM calls# 50 | This notebook covers how to cache results of individual LLM calls. 51 | 52 | 53 | import langchain 54 | from langchain.llms import OpenAI 55 | 56 | # To make the caching really obvious, lets use a slower model. 57 | llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2) 58 | 59 | 60 | 61 | 62 | 63 | In Memory Cache# 64 | 65 | 66 | from langchain.cache import InMemoryCache 67 | langchain.llm_cache = InMemoryCache() 68 | 69 | 70 | 71 | 72 | 73 | 74 | %%time 75 | # The first time, it is not yet in cache, so it should take longer 76 | llm("Tell me a joke") 77 | 78 | 79 | 80 | 81 | CPU times: user 35.9 ms, sys: 28.6 ms, total: 64.6 ms 82 | Wall time: 4.83 s 83 | 84 | 85 | "\n\nWhy couldn't the bicycle stand up by itself? It was...two tired!" 86 | 87 | 88 | 89 | 90 | 91 | 92 | %%time 93 | # The second time it is, so it goes faster 94 | llm("Tell me a joke") 95 | 96 | 97 | 98 | 99 | CPU times: user 238 µs, sys: 143 µs, total: 381 µs 100 | Wall time: 1.76 ms 101 | 102 | 103 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 104 | 105 | 106 | 107 | 108 | 109 | 110 | SQLite Cache# 111 | 112 | 113 | !rm .langchain.db 114 | 115 | 116 | 117 | 118 | 119 | 120 | # We can do the same thing with a SQLite cache 121 | from langchain.cache import SQLiteCache 122 | langchain.llm_cache = SQLiteCache(database_path=".langchain.db") 123 | 124 | 125 | 126 | 127 | 128 | 129 | %%time 130 | # The first time, it is not yet in cache, so it should take longer 131 | llm("Tell me a joke") 132 | 133 | 134 | 135 | 136 | CPU times: user 17 ms, sys: 9.76 ms, total: 26.7 ms 137 | Wall time: 825 ms 138 | 139 | 140 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 141 | 142 | 143 | 144 | 145 | 146 | 147 | %%time 148 | # The second time it is, so it goes faster 149 | llm("Tell me a joke") 150 | 151 | 152 | 153 | 154 | CPU times: user 2.46 ms, sys: 1.23 ms, total: 3.7 ms 155 | Wall time: 2.67 ms 156 | 157 | 158 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 159 | 160 | 161 | 162 | 163 | 164 | 165 | Redis Cache# 166 | 167 | Standard Cache# 168 | Use Redis to cache prompts and responses. 169 | 170 | 171 | # We can do the same thing with a Redis cache 172 | # (make sure your local Redis instance is running first before running this example) 173 | from redis import Redis 174 | from langchain.cache import RedisCache 175 | 176 | langchain.llm_cache = RedisCache(redis_=Redis()) 177 | 178 | 179 | 180 | 181 | 182 | 183 | %%time 184 | # The first time, it is not yet in cache, so it should take longer 185 | llm("Tell me a joke") 186 | 187 | 188 | 189 | 190 | CPU times: user 6.88 ms, sys: 8.75 ms, total: 15.6 ms 191 | Wall time: 1.04 s 192 | 193 | 194 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 195 | 196 | 197 | 198 | 199 | 200 | 201 | %%time 202 | # The second time it is, so it goes faster 203 | llm("Tell me a joke") 204 | 205 | 206 | 207 | 208 | CPU times: user 1.59 ms, sys: 610 µs, total: 2.2 ms 209 | Wall time: 5.58 ms 210 | 211 | 212 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 213 | 214 | 215 | 216 | 217 | 218 | 219 | Semantic Cache# 220 | Use Redis to cache prompts and responses and evaluate hits based on semantic similarity. 221 | 222 | 223 | from langchain.embeddings import OpenAIEmbeddings 224 | from langchain.cache import RedisSemanticCache 225 | 226 | 227 | langchain.llm_cache = RedisSemanticCache( 228 | redis_url="redis://localhost:6379", 229 | embedding=OpenAIEmbeddings() 230 | ) 231 | 232 | 233 | 234 | 235 | 236 | 237 | %%time 238 | # The first time, it is not yet in cache, so it should take longer 239 | llm("Tell me a joke") 240 | 241 | 242 | 243 | 244 | CPU times: user 351 ms, sys: 156 ms, total: 507 ms 245 | Wall time: 3.37 s 246 | 247 | 248 | "\n\nWhy don't scientists trust atoms?\nBecause they make up everything." 249 | 250 | 251 | 252 | 253 | 254 | 255 | %%time 256 | # The second time, while not a direct hit, the question is semantically similar to the original question, 257 | # so it uses the cached result! 258 | llm("Tell me one joke") 259 | 260 | 261 | 262 | 263 | CPU times: user 6.25 ms, sys: 2.72 ms, total: 8.97 ms 264 | Wall time: 262 ms 265 | 266 | 267 | "\n\nWhy don't scientists trust atoms?\nBecause they make up everything." 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | GPTCache# 276 | We can use GPTCache for exact match caching OR to cache results based on semantic similarity 277 | Let’s first start with an example of exact match 278 | 279 | 280 | from gptcache import Cache 281 | from gptcache.manager.factory import manager_factory 282 | from gptcache.processor.pre import get_prompt 283 | from langchain.cache import GPTCache 284 | import hashlib 285 | 286 | def get_hashed_name(name): 287 | return hashlib.sha256(name.encode()).hexdigest() 288 | 289 | def init_gptcache(cache_obj: Cache, llm: str): 290 | hashed_llm = get_hashed_name(llm) 291 | cache_obj.init( 292 | pre_embedding_func=get_prompt, 293 | data_manager=manager_factory(manager="map", data_dir=f"map_cache_{hashed_llm}"), 294 | ) 295 | 296 | langchain.llm_cache = GPTCache(init_gptcache) 297 | 298 | 299 | 300 | 301 | 302 | 303 | %%time 304 | # The first time, it is not yet in cache, so it should take longer 305 | llm("Tell me a joke") 306 | 307 | 308 | 309 | 310 | CPU times: user 21.5 ms, sys: 21.3 ms, total: 42.8 ms 311 | Wall time: 6.2 s 312 | 313 | 314 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 315 | 316 | 317 | 318 | 319 | 320 | 321 | %%time 322 | # The second time it is, so it goes faster 323 | llm("Tell me a joke") 324 | 325 | 326 | 327 | 328 | CPU times: user 571 µs, sys: 43 µs, total: 614 µs 329 | Wall time: 635 µs 330 | 331 | 332 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 333 | 334 | 335 | 336 | 337 | Let’s now show an example of similarity caching 338 | 339 | 340 | from gptcache import Cache 341 | from gptcache.adapter.api import init_similar_cache 342 | from langchain.cache import GPTCache 343 | import hashlib 344 | 345 | def get_hashed_name(name): 346 | return hashlib.sha256(name.encode()).hexdigest() 347 | 348 | def init_gptcache(cache_obj: Cache, llm: str): 349 | hashed_llm = get_hashed_name(llm) 350 | init_similar_cache(cache_obj=cache_obj, data_dir=f"similar_cache_{hashed_llm}") 351 | 352 | langchain.llm_cache = GPTCache(init_gptcache) 353 | 354 | 355 | 356 | 357 | 358 | 359 | %%time 360 | # The first time, it is not yet in cache, so it should take longer 361 | llm("Tell me a joke") 362 | 363 | 364 | 365 | 366 | CPU times: user 1.42 s, sys: 279 ms, total: 1.7 s 367 | Wall time: 8.44 s 368 | 369 | 370 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 371 | 372 | 373 | 374 | 375 | 376 | 377 | %%time 378 | # This is an exact match, so it finds it in the cache 379 | llm("Tell me a joke") 380 | 381 | 382 | 383 | 384 | CPU times: user 866 ms, sys: 20 ms, total: 886 ms 385 | Wall time: 226 ms 386 | 387 | 388 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 389 | 390 | 391 | 392 | 393 | 394 | 395 | %%time 396 | # This is not an exact match, but semantically within distance so it hits! 397 | llm("Tell me joke") 398 | 399 | 400 | 401 | 402 | CPU times: user 853 ms, sys: 14.8 ms, total: 868 ms 403 | Wall time: 224 ms 404 | 405 | 406 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' 407 | 408 | 409 | 410 | 411 | 412 | 413 | Momento Cache# 414 | Use Momento to cache prompts and responses. 415 | Requires momento to use, uncomment below to install: 416 | 417 | 418 | # !pip install momento 419 | 420 | 421 | 422 | 423 | You’ll need to get a Momento auth token to use this class. This can either be passed in to a momento.CacheClient if you’d like to instantiate that directly, as a named parameter auth_token to MomentoChatMessageHistory.from_client_params, or can just be set as an environment variable MOMENTO_AUTH_TOKEN. 424 | 425 | 426 | from datetime import timedelta 427 | 428 | from langchain.cache import MomentoCache 429 | 430 | 431 | cache_name = "langchain" 432 | ttl = timedelta(days=1) 433 | langchain.llm_cache = MomentoCache.from_client_params(cache_name, ttl) 434 | 435 | 436 | 437 | 438 | 439 | 440 | %%time 441 | # The first time, it is not yet in cache, so it should take longer 442 | llm("Tell me a joke") 443 | 444 | 445 | 446 | 447 | CPU times: user 40.7 ms, sys: 16.5 ms, total: 57.2 ms 448 | Wall time: 1.73 s 449 | 450 | 451 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 452 | 453 | 454 | 455 | 456 | 457 | 458 | %%time 459 | # The second time it is, so it goes faster 460 | # When run in the same region as the cache, latencies are single digit ms 461 | llm("Tell me a joke") 462 | 463 | 464 | 465 | 466 | CPU times: user 3.16 ms, sys: 2.98 ms, total: 6.14 ms 467 | Wall time: 57.9 ms 468 | 469 | 470 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 471 | 472 | 473 | 474 | 475 | 476 | 477 | SQLAlchemy Cache# 478 | 479 | 480 | # You can use SQLAlchemyCache to cache with any SQL database supported by SQLAlchemy. 481 | 482 | # from langchain.cache import SQLAlchemyCache 483 | # from sqlalchemy import create_engine 484 | 485 | # engine = create_engine("postgresql://postgres:postgres@localhost:5432/postgres") 486 | # langchain.llm_cache = SQLAlchemyCache(engine) 487 | 488 | 489 | 490 | 491 | 492 | Custom SQLAlchemy Schemas# 493 | 494 | 495 | # You can define your own declarative SQLAlchemyCache child class to customize the schema used for caching. For example, to support high-speed fulltext prompt indexing with Postgres, use: 496 | 497 | from sqlalchemy import Column, Integer, String, Computed, Index, Sequence 498 | from sqlalchemy import create_engine 499 | from sqlalchemy.ext.declarative import declarative_base 500 | from sqlalchemy_utils import TSVectorType 501 | from langchain.cache import SQLAlchemyCache 502 | 503 | Base = declarative_base() 504 | 505 | 506 | class FulltextLLMCache(Base): # type: ignore 507 | """Postgres table for fulltext-indexed LLM Cache""" 508 | 509 | __tablename__ = "llm_cache_fulltext" 510 | id = Column(Integer, Sequence('cache_id'), primary_key=True) 511 | prompt = Column(String, nullable=False) 512 | llm = Column(String, nullable=False) 513 | idx = Column(Integer) 514 | response = Column(String) 515 | prompt_tsv = Column(TSVectorType(), Computed("to_tsvector('english', llm || ' ' || prompt)", persisted=True)) 516 | __table_args__ = ( 517 | Index("idx_fulltext_prompt_tsv", prompt_tsv, postgresql_using="gin"), 518 | ) 519 | 520 | engine = create_engine("postgresql://postgres:postgres@localhost:5432/postgres") 521 | langchain.llm_cache = SQLAlchemyCache(engine, FulltextLLMCache) 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | Optional Caching# 530 | You can also turn off caching for specific LLMs should you choose. In the example below, even though global caching is enabled, we turn it off for a specific LLM 531 | 532 | 533 | llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2, cache=False) 534 | 535 | 536 | 537 | 538 | 539 | 540 | %%time 541 | llm("Tell me a joke") 542 | 543 | 544 | 545 | 546 | CPU times: user 5.8 ms, sys: 2.71 ms, total: 8.51 ms 547 | Wall time: 745 ms 548 | 549 | 550 | '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!' 551 | 552 | 553 | 554 | 555 | 556 | 557 | %%time 558 | llm("Tell me a joke") 559 | 560 | 561 | 562 | 563 | CPU times: user 4.91 ms, sys: 2.64 ms, total: 7.55 ms 564 | Wall time: 623 ms 565 | 566 | 567 | '\n\nTwo guys stole a calendar. They got six months each.' 568 | 569 | 570 | 571 | 572 | 573 | 574 | Optional Caching in Chains# 575 | You can also turn off caching for particular nodes in chains. Note that because of certain interfaces, its often easier to construct the chain first, and then edit the LLM afterwards. 576 | As an example, we will load a summarizer map-reduce chain. We will cache results for the map-step, but then not freeze it for the combine step. 577 | 578 | 579 | llm = OpenAI(model_name="text-davinci-002") 580 | no_cache_llm = OpenAI(model_name="text-davinci-002", cache=False) 581 | 582 | 583 | 584 | 585 | 586 | 587 | from langchain.text_splitter import CharacterTextSplitter 588 | from langchain.chains.mapreduce import MapReduceChain 589 | 590 | text_splitter = CharacterTextSplitter() 591 | 592 | 593 | 594 | 595 | 596 | 597 | with open('../../../state_of_the_union.txt') as f: 598 | state_of_the_union = f.read() 599 | texts = text_splitter.split_text(state_of_the_union) 600 | 601 | 602 | 603 | 604 | 605 | 606 | from langchain.docstore.document import Document 607 | docs = [Document(page_content=t) for t in texts[:3]] 608 | from langchain.chains.summarize import load_summarize_chain 609 | 610 | 611 | 612 | 613 | 614 | 615 | chain = load_summarize_chain(llm, chain_type="map_reduce", reduce_llm=no_cache_llm) 616 | 617 | 618 | 619 | 620 | 621 | 622 | %%time 623 | chain.run(docs) 624 | 625 | 626 | 627 | 628 | CPU times: user 452 ms, sys: 60.3 ms, total: 512 ms 629 | Wall time: 5.09 s 630 | 631 | 632 | '\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure. In response to Russian aggression in Ukraine, the United States is joining with European allies to impose sanctions and isolate Russia. American forces are being mobilized to protect NATO countries in the event that Putin decides to keep moving west. The Ukrainians are bravely fighting back, but the next few weeks will be hard for them. Putin will pay a high price for his actions in the long run. Americans should not be alarmed, as the United States is taking action to protect its interests and allies.' 633 | 634 | 635 | 636 | 637 | When we run it again, we see that it runs substantially faster but the final answer is different. This is due to caching at the map steps, but not at the reduce step. 638 | 639 | 640 | %%time 641 | chain.run(docs) 642 | 643 | 644 | 645 | 646 | CPU times: user 11.5 ms, sys: 4.33 ms, total: 15.8 ms 647 | Wall time: 1.04 s 648 | 649 | 650 | '\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure.' 651 | 652 | 653 | 654 | 655 | 656 | 657 | !rm .langchain.db sqlite.db 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | previous 672 | How (and why) to use the human input LLM 673 | 674 | 675 | 676 | 677 | next 678 | How to serialize LLM classes 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | Contents 689 | 690 | 691 | 692 | In Memory Cache 693 | SQLite Cache 694 | Redis Cache 695 | Standard Cache 696 | Semantic Cache 697 | 698 | 699 | GPTCache 700 | Momento Cache 701 | SQLAlchemy Cache 702 | Custom SQLAlchemy Schemas 703 | 704 | 705 | Optional Caching 706 | Optional Caching in Chains 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | By Harrison Chase 717 | 718 | 719 | 720 | 721 | 722 | © Copyright 2023, Harrison Chase. 723 | 724 | 725 | 726 | 727 | 728 | Last updated on Jun 14, 2023. 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | -------------------------------------------------------------------------------- /data/langchain_doc_small/2_Quickstart_Guide_Contents.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Quickstart Guide 18 | 19 | 20 | 21 | 22 | Contents 23 | 24 | 25 | 26 | Installation 27 | Environment Setup 28 | Building a Language Model Application: LLMs 29 | LLMs: Get predictions from a language model 30 | Prompt Templates: Manage prompts for LLMs 31 | Chains: Combine LLMs and prompts in multi-step workflows 32 | Agents: Dynamically Call Chains Based on User Input 33 | Memory: Add State to Chains and Agents 34 | Building a Language Model Application: Chat Models 35 | Get Message Completions from a Chat Model 36 | Chat Prompt Templates 37 | Chains with Chat Models 38 | Agents with Chat Models 39 | Memory: Add State to Chains and Agents 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | Quickstart Guide# 49 | This tutorial gives you a quick walkthrough about building an end-to-end language model application with LangChain. 50 | 51 | Installation# 52 | To get started, install LangChain with the following command: 53 | pip install langchain 54 | # or 55 | conda install langchain -c conda-forge 56 | 57 | 58 | 59 | 60 | Environment Setup# 61 | Using LangChain will usually require integrations with one or more model providers, data stores, apis, etc. 62 | For this example, we will be using OpenAI’s APIs, so we will first need to install their SDK: 63 | pip install openai 64 | 65 | 66 | We will then need to set the environment variable in the terminal. 67 | export OPENAI_API_KEY="..." 68 | 69 | 70 | Alternatively, you could do this from inside the Jupyter notebook (or Python script): 71 | import os 72 | os.environ["OPENAI_API_KEY"] = "..." 73 | 74 | 75 | If you want to set the API key dynamically, you can use the openai_api_key parameter when initiating OpenAI class—for instance, each user’s API key. 76 | from langchain.llms import OpenAI 77 | llm = OpenAI(openai_api_key="OPENAI_API_KEY") 78 | 79 | 80 | 81 | 82 | Building a Language Model Application: LLMs# 83 | Now that we have installed LangChain and set up our environment, we can start building our language model application. 84 | LangChain provides many modules that can be used to build language model applications. Modules can be combined to create more complex applications, or be used individually for simple applications. 85 | 86 | 87 | LLMs: Get predictions from a language model# 88 | The most basic building block of LangChain is calling an LLM on some input. 89 | Let’s walk through a simple example of how to do this. 90 | For this purpose, let’s pretend we are building a service that generates a company name based on what the company makes. 91 | In order to do this, we first need to import the LLM wrapper. 92 | from langchain.llms import OpenAI 93 | 94 | 95 | We can then initialize the wrapper with any arguments. 96 | In this example, we probably want the outputs to be MORE random, so we’ll initialize it with a HIGH temperature. 97 | llm = OpenAI(temperature=0.9) 98 | 99 | 100 | We can now call it on some input! 101 | text = "What would be a good company name for a company that makes colorful socks?" 102 | print(llm(text)) 103 | 104 | 105 | Feetful of Fun 106 | 107 | 108 | For more details on how to use LLMs within LangChain, see the LLM getting started guide. 109 | 110 | 111 | Prompt Templates: Manage prompts for LLMs# 112 | Calling an LLM is a great first step, but it’s just the beginning. 113 | Normally when you use an LLM in an application, you are not sending user input directly to the LLM. 114 | Instead, you are probably taking user input and constructing a prompt, and then sending that to the LLM. 115 | For example, in the previous example, the text we passed in was hardcoded to ask for a name for a company that made colorful socks. 116 | In this imaginary service, what we would want to do is take only the user input describing what the company does, and then format the prompt with that information. 117 | This is easy to do with LangChain! 118 | First lets define the prompt template: 119 | from langchain.prompts import PromptTemplate 120 | 121 | prompt = PromptTemplate( 122 | input_variables=["product"], 123 | template="What is a good name for a company that makes {product}?", 124 | ) 125 | 126 | 127 | Let’s now see how this works! We can call the .format method to format it. 128 | print(prompt.format(product="colorful socks")) 129 | 130 | 131 | What is a good name for a company that makes colorful socks? 132 | 133 | 134 | For more details, check out the getting started guide for prompts. 135 | 136 | 137 | Chains: Combine LLMs and prompts in multi-step workflows# 138 | Up until now, we’ve worked with the PromptTemplate and LLM primitives by themselves. But of course, a real application is not just one primitive, but rather a combination of them. 139 | A chain in LangChain is made up of links, which can be either primitives like LLMs or other chains. 140 | The most core type of chain is an LLMChain, which consists of a PromptTemplate and an LLM. 141 | Extending the previous example, we can construct an LLMChain which takes user input, formats it with a PromptTemplate, and then passes the formatted response to an LLM. 142 | from langchain.prompts import PromptTemplate 143 | from langchain.llms import OpenAI 144 | 145 | llm = OpenAI(temperature=0.9) 146 | prompt = PromptTemplate( 147 | input_variables=["product"], 148 | template="What is a good name for a company that makes {product}?", 149 | ) 150 | 151 | 152 | We can now create a very simple chain that will take user input, format the prompt with it, and then send it to the LLM: 153 | from langchain.chains import LLMChain 154 | chain = LLMChain(llm=llm, prompt=prompt) 155 | 156 | 157 | Now we can run that chain only specifying the product! 158 | chain.run("colorful socks") 159 | # -> '\n\nSocktastic!' 160 | 161 | 162 | There we go! There’s the first chain - an LLM Chain. 163 | This is one of the simpler types of chains, but understanding how it works will set you up well for working with more complex chains. 164 | For more details, check out the getting started guide for chains. 165 | 166 | 167 | Agents: Dynamically Call Chains Based on User Input# 168 | So far the chains we’ve looked at run in a predetermined order. 169 | Agents no longer do: they use an LLM to determine which actions to take and in what order. An action can either be using a tool and observing its output, or returning to the user. 170 | When used correctly agents can be extremely powerful. In this tutorial, we show you how to easily use agents through the simplest, highest level API. 171 | In order to load agents, you should understand the following concepts: 172 | 173 | Tool: A function that performs a specific duty. This can be things like: Google Search, Database lookup, Python REPL, other chains. The interface for a tool is currently a function that is expected to have a string as an input, with a string as an output. 174 | LLM: The language model powering the agent. 175 | Agent: The agent to use. This should be a string that references a support agent class. Because this notebook focuses on the simplest, highest level API, this only covers using the standard supported agents. If you want to implement a custom agent, see the documentation for custom agents (coming soon). 176 | 177 | Agents: For a list of supported agents and their specifications, see here. 178 | Tools: For a list of predefined tools and their specifications, see here. 179 | For this example, you will also need to install the SerpAPI Python package. 180 | pip install google-search-results 181 | 182 | 183 | And set the appropriate environment variables. 184 | import os 185 | os.environ["SERPAPI_API_KEY"] = "..." 186 | 187 | 188 | Now we can get started! 189 | from langchain.agents import load_tools 190 | from langchain.agents import initialize_agent 191 | from langchain.agents import AgentType 192 | from langchain.llms import OpenAI 193 | 194 | # First, let's load the language model we're going to use to control the agent. 195 | llm = OpenAI(temperature=0) 196 | 197 | # Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in. 198 | tools = load_tools(["serpapi", "llm-math"], llm=llm) 199 | 200 | 201 | # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use. 202 | agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 203 | 204 | # Now let's test it out! 205 | agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?") 206 | 207 | 208 | > Entering new AgentExecutor chain... 209 | I need to find the temperature first, then use the calculator to raise it to the .023 power. 210 | Action: Search 211 | Action Input: "High temperature in SF yesterday" 212 | Observation: San Francisco Temperature Yesterday. Maximum temperature yesterday: 57 °F (at 1:56 pm) Minimum temperature yesterday: 49 °F (at 1:56 am) Average temperature ... 213 | Thought: I now have the temperature, so I can use the calculator to raise it to the .023 power. 214 | Action: Calculator 215 | Action Input: 57^.023 216 | Observation: Answer: 1.0974509573251117 217 | 218 | Thought: I now know the final answer 219 | Final Answer: The high temperature in SF yesterday in Fahrenheit raised to the .023 power is 1.0974509573251117. 220 | 221 | > Finished chain. 222 | 223 | 224 | 225 | 226 | Memory: Add State to Chains and Agents# 227 | So far, all the chains and agents we’ve gone through have been stateless. But often, you may want a chain or agent to have some concept of “memory” so that it may remember information about its previous interactions. The clearest and simple example of this is when designing a chatbot - you want it to remember previous messages so it can use context from that to have a better conversation. This would be a type of “short-term memory”. On the more complex side, you could imagine a chain/agent remembering key pieces of information over time - this would be a form of “long-term memory”. For more concrete ideas on the latter, see this awesome paper. 228 | LangChain provides several specially created chains just for this purpose. This notebook walks through using one of those chains (the ConversationChain) with two different types of memory. 229 | By default, the ConversationChain has a simple type of memory that remembers all previous inputs/outputs and adds them to the context that is passed. Let’s take a look at using this chain (setting verbose=True so we can see the prompt). 230 | from langchain import OpenAI, ConversationChain 231 | 232 | llm = OpenAI(temperature=0) 233 | conversation = ConversationChain(llm=llm, verbose=True) 234 | 235 | output = conversation.predict(input="Hi there!") 236 | print(output) 237 | 238 | 239 | > Entering new chain... 240 | Prompt after formatting: 241 | The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. 242 | 243 | Current conversation: 244 | 245 | Human: Hi there! 246 | AI: 247 | 248 | > Finished chain. 249 | ' Hello! How are you today?' 250 | 251 | 252 | output = conversation.predict(input="I'm doing well! Just having a conversation with an AI.") 253 | print(output) 254 | 255 | 256 | > Entering new chain... 257 | Prompt after formatting: 258 | The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. 259 | 260 | Current conversation: 261 | 262 | Human: Hi there! 263 | AI: Hello! How are you today? 264 | Human: I'm doing well! Just having a conversation with an AI. 265 | AI: 266 | 267 | > Finished chain. 268 | " That's great! What would you like to talk about?" 269 | 270 | 271 | 272 | 273 | Building a Language Model Application: Chat Models# 274 | Similarly, you can use chat models instead of LLMs. Chat models are a variation on language models. While chat models use language models under the hood, the interface they expose is a bit different: rather than expose a “text in, text out” API, they expose an interface where “chat messages” are the inputs and outputs. 275 | Chat model APIs are fairly new, so we are still figuring out the correct abstractions. 276 | 277 | 278 | Get Message Completions from a Chat Model# 279 | You can get chat completions by passing one or more messages to the chat model. The response will be a message. The types of messages currently supported in LangChain are AIMessage, HumanMessage, SystemMessage, and ChatMessage – ChatMessage takes in an arbitrary role parameter. Most of the time, you’ll just be dealing with HumanMessage, AIMessage, and SystemMessage. 280 | from langchain.chat_models import ChatOpenAI 281 | from langchain.schema import ( 282 | AIMessage, 283 | HumanMessage, 284 | SystemMessage 285 | ) 286 | 287 | chat = ChatOpenAI(temperature=0) 288 | 289 | 290 | You can get completions by passing in a single message. 291 | chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")]) 292 | # -> AIMessage(content="J'aime programmer.", additional_kwargs={}) 293 | 294 | 295 | You can also pass in multiple messages for OpenAI’s gpt-3.5-turbo and gpt-4 models. 296 | messages = [ 297 | SystemMessage(content="You are a helpful assistant that translates English to French."), 298 | HumanMessage(content="I love programming.") 299 | ] 300 | chat(messages) 301 | # -> AIMessage(content="J'aime programmer.", additional_kwargs={}) 302 | 303 | 304 | You can go one step further and generate completions for multiple sets of messages using generate. This returns an LLMResult with an additional message parameter: 305 | batch_messages = [ 306 | [ 307 | SystemMessage(content="You are a helpful assistant that translates English to French."), 308 | HumanMessage(content="I love programming.") 309 | ], 310 | [ 311 | SystemMessage(content="You are a helpful assistant that translates English to French."), 312 | HumanMessage(content="I love artificial intelligence.") 313 | ], 314 | ] 315 | result = chat.generate(batch_messages) 316 | result 317 | # -> LLMResult(generations=[[ChatGeneration(text="J'aime programmer.", generation_info=None, message=AIMessage(content="J'aime programmer.", additional_kwargs={}))], [ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info=None, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}}) 318 | 319 | 320 | You can recover things like token usage from this LLMResult: 321 | result.llm_output['token_usage'] 322 | # -> {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77} 323 | 324 | 325 | 326 | 327 | Chat Prompt Templates# 328 | Similar to LLMs, you can make use of templating by using a MessagePromptTemplate. You can build a ChatPromptTemplate from one or more MessagePromptTemplates. You can use ChatPromptTemplate’s format_prompt – this returns a PromptValue, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an llm or chat model. 329 | For convenience, there is a from_template method exposed on the template. If you were to use this template, this is what it would look like: 330 | from langchain.chat_models import ChatOpenAI 331 | from langchain.prompts.chat import ( 332 | ChatPromptTemplate, 333 | SystemMessagePromptTemplate, 334 | HumanMessagePromptTemplate, 335 | ) 336 | 337 | chat = ChatOpenAI(temperature=0) 338 | 339 | template = "You are a helpful assistant that translates {input_language} to {output_language}." 340 | system_message_prompt = SystemMessagePromptTemplate.from_template(template) 341 | human_template = "{text}" 342 | human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) 343 | 344 | chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) 345 | 346 | # get a chat completion from the formatted messages 347 | chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()) 348 | # -> AIMessage(content="J'aime programmer.", additional_kwargs={}) 349 | 350 | 351 | 352 | 353 | Chains with Chat Models# 354 | The LLMChain discussed in the above section can be used with chat models as well: 355 | from langchain.chat_models import ChatOpenAI 356 | from langchain import LLMChain 357 | from langchain.prompts.chat import ( 358 | ChatPromptTemplate, 359 | SystemMessagePromptTemplate, 360 | HumanMessagePromptTemplate, 361 | ) 362 | 363 | chat = ChatOpenAI(temperature=0) 364 | 365 | template = "You are a helpful assistant that translates {input_language} to {output_language}." 366 | system_message_prompt = SystemMessagePromptTemplate.from_template(template) 367 | human_template = "{text}" 368 | human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) 369 | chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) 370 | 371 | chain = LLMChain(llm=chat, prompt=chat_prompt) 372 | chain.run(input_language="English", output_language="French", text="I love programming.") 373 | # -> "J'aime programmer." 374 | 375 | 376 | 377 | 378 | Agents with Chat Models# 379 | Agents can also be used with chat models, you can initialize one using AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION as the agent type. 380 | from langchain.agents import load_tools 381 | from langchain.agents import initialize_agent 382 | from langchain.agents import AgentType 383 | from langchain.chat_models import ChatOpenAI 384 | from langchain.llms import OpenAI 385 | 386 | # First, let's load the language model we're going to use to control the agent. 387 | chat = ChatOpenAI(temperature=0) 388 | 389 | # Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in. 390 | llm = OpenAI(temperature=0) 391 | tools = load_tools(["serpapi", "llm-math"], llm=llm) 392 | 393 | 394 | # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use. 395 | agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True) 396 | 397 | # Now let's test it out! 398 | agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?") 399 | 400 | 401 | 402 | > Entering new AgentExecutor chain... 403 | Thought: I need to use a search engine to find Olivia Wilde's boyfriend and a calculator to raise his age to the 0.23 power. 404 | Action: 405 | { 406 | "action": "Search", 407 | "action_input": "Olivia Wilde boyfriend" 408 | } 409 | 410 | Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. 411 | Thought:I need to use a search engine to find Harry Styles' current age. 412 | Action: 413 | { 414 | "action": "Search", 415 | "action_input": "Harry Styles age" 416 | } 417 | 418 | Observation: 29 years 419 | Thought:Now I need to calculate 29 raised to the 0.23 power. 420 | Action: 421 | { 422 | "action": "Calculator", 423 | "action_input": "29^0.23" 424 | } 425 | 426 | Observation: Answer: 2.169459462491557 427 | 428 | Thought:I now know the final answer. 429 | Final Answer: 2.169459462491557 430 | 431 | > Finished chain. 432 | '2.169459462491557' 433 | 434 | 435 | 436 | 437 | Memory: Add State to Chains and Agents# 438 | You can use Memory with chains and agents initialized with chat models. The main difference between this and Memory for LLMs is that rather than trying to condense all previous messages into a string, we can keep them as their own unique memory object. 439 | from langchain.prompts import ( 440 | ChatPromptTemplate, 441 | MessagesPlaceholder, 442 | SystemMessagePromptTemplate, 443 | HumanMessagePromptTemplate 444 | ) 445 | from langchain.chains import ConversationChain 446 | from langchain.chat_models import ChatOpenAI 447 | from langchain.memory import ConversationBufferMemory 448 | 449 | prompt = ChatPromptTemplate.from_messages([ 450 | SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know."), 451 | MessagesPlaceholder(variable_name="history"), 452 | HumanMessagePromptTemplate.from_template("{input}") 453 | ]) 454 | 455 | llm = ChatOpenAI(temperature=0) 456 | memory = ConversationBufferMemory(return_messages=True) 457 | conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm) 458 | 459 | conversation.predict(input="Hi there!") 460 | # -> 'Hello! How can I assist you today?' 461 | 462 | 463 | conversation.predict(input="I'm doing well! Just having a conversation with an AI.") 464 | # -> "That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?" 465 | 466 | conversation.predict(input="Tell me about yourself.") 467 | # -> "Sure! I am an AI language model created by OpenAI. I was trained on a large dataset of text from the internet, which allows me to understand and generate human-like language. I can answer questions, provide information, and even have conversations like this one. Is there anything else you'd like to know about me?" 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | previous 480 | Welcome to LangChain 481 | 482 | 483 | 484 | 485 | next 486 | Concepts 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | Contents 497 | 498 | 499 | 500 | Installation 501 | Environment Setup 502 | Building a Language Model Application: LLMs 503 | LLMs: Get predictions from a language model 504 | Prompt Templates: Manage prompts for LLMs 505 | Chains: Combine LLMs and prompts in multi-step workflows 506 | Agents: Dynamically Call Chains Based on User Input 507 | Memory: Add State to Chains and Agents 508 | Building a Language Model Application: Chat Models 509 | Get Message Completions from a Chat Model 510 | Chat Prompt Templates 511 | Chains with Chat Models 512 | Agents with Chat Models 513 | Memory: Add State to Chains and Agents 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | By Harrison Chase 524 | 525 | 526 | 527 | 528 | 529 | © Copyright 2023, Harrison Chase. 530 | 531 | 532 | 533 | 534 | 535 | Last updated on Jun 14, 2023. 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | -------------------------------------------------------------------------------- /ChromaDB.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "id": "initial_id", 7 | "metadata": { 8 | "collapsed": true, 9 | "ExecuteTime": { 10 | "end_time": "2023-10-22T13:46:08.320713Z", 11 | "start_time": "2023-10-22T13:46:08.318972Z" 12 | } 13 | }, 14 | "outputs": [], 15 | "source": [ 16 | "# Import libraries\n", 17 | "from langchain.vectorstores import Chroma\n", 18 | "from langchain.chains import RetrievalQA\n", 19 | "from langchain.embeddings import OpenAIEmbeddings\n", 20 | "import os\n", 21 | "\n", 22 | "#Ollama\n", 23 | "from langchain.llms import Ollama\n", 24 | "from langchain.callbacks.manager import CallbackManager\n", 25 | "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", 26 | "from langchain.embeddings import OllamaEmbeddings\n", 27 | "from langchain.prompts import PromptTemplate\n", 28 | "from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader, PyPDFLoader\n", 29 | "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", 30 | "from langchain.document_loaders import DirectoryLoader\n" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 48, 36 | "outputs": [], 37 | "source": [ 38 | "# Set the API key when using OpenAI embeddings\n", 39 | "os.environ[\"OPENAI_API_KEY\"] = \"sk-...\"" 40 | ], 41 | "metadata": { 42 | "collapsed": false, 43 | "ExecuteTime": { 44 | "end_time": "2023-10-22T14:10:07.764179Z", 45 | "start_time": "2023-10-22T14:10:07.760696Z" 46 | } 47 | }, 48 | "id": "19aa4507398334c6" 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "source": [ 53 | "# Load the embeddings and the model\n", 54 | "For this notebook i will use the Mistral 7B model and the Ollama embeddings. You can also use the OpenAI embeddings." 55 | ], 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "id": "d516b0ea1eef2caa" 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 70, 64 | "outputs": [], 65 | "source": [ 66 | "# Ollama embeddings\n", 67 | "embeddings_open = OllamaEmbeddings(model=\"mistral\")\n", 68 | "# OpenAI embeddings\n", 69 | "#embedding = OpenAIEmbeddings()\n", 70 | "\n", 71 | "llm_open = Ollama( model=\"mistral\",\n", 72 | " #model='Llama2',\n", 73 | " callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]))" 74 | ], 75 | "metadata": { 76 | "collapsed": false, 77 | "ExecuteTime": { 78 | "end_time": "2023-10-22T14:13:11.219Z", 79 | "start_time": "2023-10-22T14:13:11.211783Z" 80 | } 81 | }, 82 | "id": "31f873210b0f2bb6" 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "source": [ 87 | "# Load the data \n", 88 | "Load the data from the directory and split it into chunks. " 89 | ], 90 | "metadata": { 91 | "collapsed": false 92 | }, 93 | "id": "b004ca4c4196e842" 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 50, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": "41" 102 | }, 103 | "execution_count": 50, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "from langchain.document_loaders import DirectoryLoader\n", 110 | "\n", 111 | "# Print number of txt files in directory\n", 112 | "loader = DirectoryLoader('../data/langchain_doc_small', glob=\"./*.txt\")\n", 113 | "\n", 114 | "# load pdfs from directory and print number of pdfs\n", 115 | "#loader = PyPDFLoader('../data/PDFs/How_to_build_your_carreer_in_AI.pdf')\n", 116 | "\n", 117 | "# load another file directly\n", 118 | "#loader = DirectoryLoader('/your/path/to/file.txt')\n", 119 | "\n", 120 | "doc = loader.load ( )\n", 121 | "len(doc)" 122 | ], 123 | "metadata": { 124 | "collapsed": false, 125 | "ExecuteTime": { 126 | "end_time": "2023-10-22T14:10:13.263732Z", 127 | "start_time": "2023-10-22T14:10:11.537487Z" 128 | } 129 | }, 130 | "id": "381e27a6499e6d71" 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "source": [ 135 | "# Print the first document" 136 | ], 137 | "metadata": { 138 | "collapsed": false 139 | }, 140 | "id": "172ffbcf0eae8b99" 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 51, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "page_content='Aleph Alpha\\n\\nAleph Alpha# The Luminous series is a family of large language models. This example goes over how to use LangChain to interact with Aleph Alpha models\\n\\n# Install the package\\n\\n!pip install aleph\\n\\n\\n\\nalpha\\n\\n\\n\\nclient\\n\\n# create a new token: https://docs.aleph-alpha.com/docs/account/#create-a-new-token\\n\\nfrom getpass import getpass\\n\\nALEPH_ALPHA_API_KEY = getpass()\\n\\nfrom langchain.llms import AlephAlpha from langchain import PromptTemplate, LLMChain\\n\\ntemplate = \"\"\"Q: {question}\\n\\nA:\"\"\"\\n\\nprompt = PromptTemplate(template=template, input_variables=[\"question\"])\\n\\nllm = AlephAlpha(model=\"luminous\\n\\n\\n\\nextended\", maximum_tokens=20, stop_sequences=[\"Q:\"], aleph_alpha_api_key=ALEPH_ALPHA_API_KEY)\\n\\nllm_chain = LLMChain(prompt=prompt, llm=llm)\\n\\nquestion = \"What is AI?\"\\n\\nllm_chain.run(question)\\n\\n\\' Artificial Intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems.\\\\n\\'\\n\\nprevious\\n\\nAI21\\n\\nnext\\n\\nAnyscale\\n\\nBy Harrison Chase\\n\\n© Copyright 2023, Harrison Chase.\\n\\nLast updated on Jun 13, 2023.' metadata={'source': '/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/20_Aleph_Alpha_Aleph.txt'}\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "print(doc[0])" 156 | ], 157 | "metadata": { 158 | "collapsed": false, 159 | "ExecuteTime": { 160 | "end_time": "2023-10-22T14:10:13.265066Z", 161 | "start_time": "2023-10-22T14:10:13.263071Z" 162 | } 163 | }, 164 | "id": "dc0aad101a41700c" 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "source": [ 169 | "# Split the text into chunks" 170 | ], 171 | "metadata": { 172 | "collapsed": false 173 | }, 174 | "id": "df1a5e0be0624637" 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 52, 179 | "outputs": [], 180 | "source": [ 181 | "# Splitting the text into chunks\n", 182 | "text_splitter = RecursiveCharacterTextSplitter (chunk_size=500, chunk_overlap=50)\n", 183 | "texts = text_splitter.split_documents(doc)" 184 | ], 185 | "metadata": { 186 | "collapsed": false, 187 | "ExecuteTime": { 188 | "end_time": "2023-10-22T14:10:13.613910Z", 189 | "start_time": "2023-10-22T14:10:13.608962Z" 190 | } 191 | }, 192 | "id": "b7fac36b7b5690f5" 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "source": [ 197 | "# Count the number of chunks" 198 | ], 199 | "metadata": { 200 | "collapsed": false 201 | }, 202 | "id": "6921794ad8962108" 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 53, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": "378" 211 | }, 212 | "execution_count": 53, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "len(texts)" 219 | ], 220 | "metadata": { 221 | "collapsed": false, 222 | "ExecuteTime": { 223 | "end_time": "2023-10-22T14:10:14.484937Z", 224 | "start_time": "2023-10-22T14:10:14.480689Z" 225 | } 226 | }, 227 | "id": "a40c8b5d391da327" 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "source": [ 232 | "# Print the first chunk" 233 | ], 234 | "metadata": { 235 | "collapsed": false 236 | }, 237 | "id": "d10b6fb845bc037" 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 54, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": "Document(page_content='Aleph Alpha\\n\\nAleph Alpha# The Luminous series is a family of large language models. This example goes over how to use LangChain to interact with Aleph Alpha models\\n\\n# Install the package\\n\\n!pip install aleph\\n\\n\\n\\nalpha\\n\\n\\n\\nclient\\n\\n# create a new token: https://docs.aleph-alpha.com/docs/account/#create-a-new-token\\n\\nfrom getpass import getpass\\n\\nALEPH_ALPHA_API_KEY = getpass()\\n\\nfrom langchain.llms import AlephAlpha from langchain import PromptTemplate, LLMChain\\n\\ntemplate = \"\"\"Q: {question}\\n\\nA:\"\"\"', metadata={'source': '/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/20_Aleph_Alpha_Aleph.txt'})" 246 | }, 247 | "execution_count": 54, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "texts[0]" 254 | ], 255 | "metadata": { 256 | "collapsed": false, 257 | "ExecuteTime": { 258 | "end_time": "2023-10-22T14:10:15.422234Z", 259 | "start_time": "2023-10-22T14:10:15.417849Z" 260 | } 261 | }, 262 | "id": "6ed4c59383cdf49c" 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "source": [ 267 | "# Embed and store the texts\n", 268 | "Supplying a persist_directory will store the embeddings on disk, so that they can be loaded later." 269 | ], 270 | "metadata": { 271 | "collapsed": false 272 | }, 273 | "id": "ce407d89f1110a05" 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 55, 278 | "outputs": [], 279 | "source": [ 280 | "# PDFs from directory\n", 281 | "#persist_directory = 'PDFs_How_to_build_your_carreer_in_AI'\n", 282 | "\n", 283 | "# Langchain documentation\n", 284 | "persist_directory = 'vdb_langchain_doc_small'\n", 285 | "\n", 286 | "# Your documents \n", 287 | "#persist_directory = 'your_new_database'\n", 288 | "\n", 289 | "vectordb = Chroma.from_documents(documents=texts,\n", 290 | " \n", 291 | " # Chose the embedding you want to use\n", 292 | " # embedding=embeddings_open,\n", 293 | " embedding=embeddings_open,\n", 294 | " \n", 295 | " persist_directory=persist_directory)" 296 | ], 297 | "metadata": { 298 | "collapsed": false, 299 | "ExecuteTime": { 300 | "end_time": "2023-10-22T14:10:20.077716Z", 301 | "start_time": "2023-10-22T14:10:17.208717Z" 302 | } 303 | }, 304 | "id": "d0caf622e36bc7c7" 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "source": [ 309 | "# Save to disc\n" 310 | ], 311 | "metadata": { 312 | "collapsed": false 313 | }, 314 | "id": "fe0b87a8f3b1ab1a" 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 56, 319 | "outputs": [], 320 | "source": [ 321 | "# Persist the db to disk\n", 322 | "vectordb.persist()\n", 323 | "vectordb = None" 324 | ], 325 | "metadata": { 326 | "collapsed": false, 327 | "ExecuteTime": { 328 | "end_time": "2023-10-22T14:10:21.985550Z", 329 | "start_time": "2023-10-22T14:10:21.980419Z" 330 | } 331 | }, 332 | "id": "3b1d92ce6d0af525" 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "source": [ 337 | "# Choose persistence directory and load from disk" 338 | ], 339 | "metadata": { 340 | "collapsed": false 341 | }, 342 | "id": "a369c7fa73f32fe6" 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 57, 347 | "outputs": [], 348 | "source": [ 349 | "# Langchain documentation\n", 350 | "persist_directory = 'vdb_langchain_doc_small'\n", 351 | "\n", 352 | "# PDFs from directory\n", 353 | "#persist_directory = 'PDFs_How_to_build_your_carreer_in_AI'" 354 | ], 355 | "metadata": { 356 | "collapsed": false, 357 | "ExecuteTime": { 358 | "end_time": "2023-10-22T14:10:34.951964Z", 359 | "start_time": "2023-10-22T14:10:34.948316Z" 360 | } 361 | }, 362 | "id": "21727fbc2cef3b6a" 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "source": [ 367 | "# Now we can load the persisted database from disk, and use it as normal." 368 | ], 369 | "metadata": { 370 | "collapsed": false 371 | }, 372 | "id": "da269fc5e9808fc4" 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 58, 377 | "outputs": [], 378 | "source": [ 379 | "vectordb = Chroma(persist_directory=persist_directory,\n", 380 | " embedding_function=embeddings_open\n", 381 | " #embedding_function=embedding\n", 382 | " )" 383 | ], 384 | "metadata": { 385 | "collapsed": false, 386 | "ExecuteTime": { 387 | "end_time": "2023-10-22T14:10:37.062806Z", 388 | "start_time": "2023-10-22T14:10:37.050960Z" 389 | } 390 | }, 391 | "id": "f012c910a6504b8b" 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "source": [ 396 | "# Create the retriever" 397 | ], 398 | "metadata": { 399 | "collapsed": false 400 | }, 401 | "id": "5e77acb94af62830" 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 59, 406 | "outputs": [], 407 | "source": [ 408 | "retriever = vectordb.as_retriever()" 409 | ], 410 | "metadata": { 411 | "collapsed": false, 412 | "ExecuteTime": { 413 | "end_time": "2023-10-22T14:10:38.243239Z", 414 | "start_time": "2023-10-22T14:10:38.233437Z" 415 | } 416 | }, 417 | "id": "dcc8e33565f576dc" 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 60, 422 | "outputs": [], 423 | "source": [ 424 | "docs = retriever.get_relevant_documents(\"What is this document about?\")" 425 | ], 426 | "metadata": { 427 | "collapsed": false, 428 | "ExecuteTime": { 429 | "end_time": "2023-10-22T14:10:39.857550Z", 430 | "start_time": "2023-10-22T14:10:39.467642Z" 431 | } 432 | }, 433 | "id": "ccd5f8b671622d9a" 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "source": [ 438 | "# Print the number of documents that are returned" 439 | ], 440 | "metadata": { 441 | "collapsed": false 442 | }, 443 | "id": "31727f127da2e2f1" 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 61, 448 | "outputs": [ 449 | { 450 | "data": { 451 | "text/plain": "4" 452 | }, 453 | "execution_count": 61, 454 | "metadata": {}, 455 | "output_type": "execute_result" 456 | } 457 | ], 458 | "source": [ 459 | "docs\n", 460 | "len(docs)" 461 | ], 462 | "metadata": { 463 | "collapsed": false, 464 | "ExecuteTime": { 465 | "end_time": "2023-10-22T14:10:40.542775Z", 466 | "start_time": "2023-10-22T14:10:40.537340Z" 467 | } 468 | }, 469 | "id": "d5b5b15ffee9a9a2" 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "source": [ 474 | "# Cite sources" 475 | ], 476 | "metadata": { 477 | "collapsed": false 478 | }, 479 | "id": "9e8652ea0c04d52e" 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 62, 484 | "outputs": [], 485 | "source": [ 486 | "def process_llm_response(llm_response):\n", 487 | " print(llm_response['result'])\n", 488 | " print('\\n\\nSources:')\n", 489 | " for source in llm_response[\"source_documents\"]:\n", 490 | " print(source.metadata['source'])" 491 | ], 492 | "metadata": { 493 | "collapsed": false, 494 | "ExecuteTime": { 495 | "end_time": "2023-10-22T14:10:41.439707Z", 496 | "start_time": "2023-10-22T14:10:41.435163Z" 497 | } 498 | }, 499 | "id": "22f6c67728e4116" 500 | }, 501 | { 502 | "cell_type": "markdown", 503 | "source": [ 504 | "# Create the chain to answer questions" 505 | ], 506 | "metadata": { 507 | "collapsed": false 508 | }, 509 | "id": "8745711b022aca6f" 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 63, 514 | "outputs": [], 515 | "source": [ 516 | "qa_chain = RetrievalQA.from_chain_type(llm=llm_open,\n", 517 | " chain_type=\"stuff\",\n", 518 | " retriever=retriever,\n", 519 | " return_source_documents=True,\n", 520 | " verbose=True)" 521 | ], 522 | "metadata": { 523 | "collapsed": false, 524 | "ExecuteTime": { 525 | "end_time": "2023-10-22T14:10:42.349496Z", 526 | "start_time": "2023-10-22T14:10:42.345521Z" 527 | } 528 | }, 529 | "id": "3ff137a6d5dc18a7" 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "source": [ 534 | "# Question" 535 | ], 536 | "metadata": { 537 | "collapsed": false 538 | }, 539 | "id": "50c26eb8f42b29e1" 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 64, 544 | "outputs": [ 545 | { 546 | "name": "stdout", 547 | "output_type": "stream", 548 | "text": [ 549 | "\n", 550 | "\n", 551 | "\u001B[1m> Entering new RetrievalQA chain...\u001B[0m\n", 552 | "This document appears to be a Quickstart Guide for getting started with a software product or service. It contains sections on Getting Started, Modules, Use Cases, Reference Docs, and Ecosystem, as well as Additional Resources.\n", 553 | "\u001B[1m> Finished chain.\u001B[0m\n", 554 | "This document appears to be a Quickstart Guide for getting started with a software product or service. It contains sections on Getting Started, Modules, Use Cases, Reference Docs, and Ecosystem, as well as Additional Resources.\n", 555 | "\n", 556 | "\n", 557 | "Sources:\n", 558 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/2_Quickstart_Guide_Contents.txt\n", 559 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/8_Getting_Started_Getting.txt\n", 560 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/7_LLMs_LLMs_Note.txt\n", 561 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/0_Welcome_to_LangChain.txt\n" 562 | ] 563 | } 564 | ], 565 | "source": [ 566 | "# Question\n", 567 | "query = \"What is this document about?\"\n", 568 | "llm_response = qa_chain(query)\n", 569 | "process_llm_response(llm_response)" 570 | ], 571 | "metadata": { 572 | "collapsed": false, 573 | "ExecuteTime": { 574 | "end_time": "2023-10-22T14:10:51.532368Z", 575 | "start_time": "2023-10-22T14:10:43.252111Z" 576 | } 577 | }, 578 | "id": "a93117b6c901b0fd" 579 | }, 580 | { 581 | "cell_type": "markdown", 582 | "source": [ 583 | "# Create a prompt template to use in the chain " 584 | ], 585 | "metadata": { 586 | "collapsed": false 587 | }, 588 | "id": "b0b86c74c46570ef" 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": 65, 593 | "outputs": [], 594 | "source": [ 595 | "def build_prompt(template_num=\"template_1\"):\n", 596 | " template = \"\"\" You are a helpful chatbot, named RSLT. You answer the questions of the customers giving a lot of details based on what you find in the context.\n", 597 | "Do not say anything that is not in the website\n", 598 | "You are to act as though you're having a conversation with a human.\n", 599 | "You are only able to answer questions, guide and assist, and provide recommendations to users. You cannot perform any other tasks outside of this.\n", 600 | "Your tone should be professional and friendly.\n", 601 | "Your purpose is to answer questions people might have, however if the question is unethical you can choose not to answer it.\n", 602 | "Your responses should always be one paragraph long or less.\n", 603 | " Context: {context}\n", 604 | " Question: {question}\n", 605 | " Helpful Answer:\"\"\"\n", 606 | "\n", 607 | " template2 = \"\"\"You are a helpful chatbot, named RSLT. You answer the questions of the customers giving a lot of details based on what you find in the context. \n", 608 | " Your responses should always be one paragraph long or less.\n", 609 | " Question: {question}\n", 610 | " Helpful Answer:\"\"\"\n", 611 | "\n", 612 | " if template_num == \"template_1\":\n", 613 | " prompt = PromptTemplate(input_variables=[\"context\", \"question\"], template=template)\n", 614 | " return prompt\n", 615 | "\n", 616 | " elif template_num == \"template_2\":\n", 617 | " prompt = PromptTemplate(input_variables=[\"question\"], template=template2)\n", 618 | " return prompt\n", 619 | "\n", 620 | " else:\n", 621 | " print(\"Please choose a valid template\")" 622 | ], 623 | "metadata": { 624 | "collapsed": false, 625 | "ExecuteTime": { 626 | "end_time": "2023-10-22T14:10:58.421712Z", 627 | "start_time": "2023-10-22T14:10:58.417481Z" 628 | } 629 | }, 630 | "id": "75cee000aa10d601" 631 | }, 632 | { 633 | "cell_type": "markdown", 634 | "source": [ 635 | "# Create the chain to answer questions" 636 | ], 637 | "metadata": { 638 | "collapsed": false 639 | }, 640 | "id": "cf9408c72c79e80a" 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": 66, 645 | "outputs": [], 646 | "source": [ 647 | "qa_chain = RetrievalQA.from_chain_type(llm=llm_open,\n", 648 | " chain_type=\"stuff\",\n", 649 | " retriever=retriever,\n", 650 | " return_source_documents=True,\n", 651 | " verbose=True,\n", 652 | " chain_type_kwargs={\"prompt\": build_prompt(\"template_1\")})" 653 | ], 654 | "metadata": { 655 | "collapsed": false, 656 | "ExecuteTime": { 657 | "end_time": "2023-10-22T14:11:00.705541Z", 658 | "start_time": "2023-10-22T14:11:00.701934Z" 659 | } 660 | }, 661 | "id": "84d64a45a91aa4c" 662 | }, 663 | { 664 | "cell_type": "markdown", 665 | "source": [ 666 | "# Question\n" 667 | ], 668 | "metadata": { 669 | "collapsed": false 670 | }, 671 | "id": "964035d669f039a2" 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 67, 676 | "outputs": [ 677 | { 678 | "name": "stdout", 679 | "output_type": "stream", 680 | "text": [ 681 | "\n", 682 | "\n", 683 | "\u001B[1m> Entering new RetrievalQA chain...\u001B[0m\n", 684 | "This document appears to be a guide for users who want to quickly get started with a specific software or system. The guide is divided into several sections, including Getting Started, Modules, Use Cases, Reference Docs, Ecosystem, and Additional Resources. It seems that the guide provides an overview of the features and functionalities of the software, as well as some practical examples of how to use it in different contexts. The author of the guide is Harrison Chase, and it was last updated on June 14, 2023.\n", 685 | "\u001B[1m> Finished chain.\u001B[0m\n", 686 | "This document appears to be a guide for users who want to quickly get started with a specific software or system. The guide is divided into several sections, including Getting Started, Modules, Use Cases, Reference Docs, Ecosystem, and Additional Resources. It seems that the guide provides an overview of the features and functionalities of the software, as well as some practical examples of how to use it in different contexts. The author of the guide is Harrison Chase, and it was last updated on June 14, 2023.\n", 687 | "\n", 688 | "\n", 689 | "Sources:\n", 690 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/2_Quickstart_Guide_Contents.txt\n", 691 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/8_Getting_Started_Getting.txt\n", 692 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/7_LLMs_LLMs_Note.txt\n", 693 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/0_Welcome_to_LangChain.txt\n" 694 | ] 695 | } 696 | ], 697 | "source": [ 698 | "query = \"What is this document about?\"\n", 699 | "llm_response = qa_chain(query)\n", 700 | "process_llm_response(llm_response)" 701 | ], 702 | "metadata": { 703 | "collapsed": false, 704 | "ExecuteTime": { 705 | "end_time": "2023-10-22T14:11:06.994282Z", 706 | "start_time": "2023-10-22T14:11:01.264850Z" 707 | } 708 | }, 709 | "id": "844c66124849bc0f" 710 | }, 711 | { 712 | "cell_type": "markdown", 713 | "source": [ 714 | "# Continue question" 715 | ], 716 | "metadata": { 717 | "collapsed": false 718 | }, 719 | "id": "7f6301e5032c792f" 720 | }, 721 | { 722 | "cell_type": "code", 723 | "execution_count": 68, 724 | "outputs": [ 725 | { 726 | "name": "stdout", 727 | "output_type": "stream", 728 | "text": [ 729 | "\n", 730 | "\n", 731 | "\u001B[1m> Entering new RetrievalQA chain...\u001B[0m\n", 732 | "LangChain is a framework for developing applications powered by language models. It enables developers to build data-aware and agentic applications that can call out to a language model, connect it to other sources of data, and allow it to interact with its environment. LangChain includes a variety of modules, use cases, reference documents, an ecosystem, and additional resources to support the development of these applications.\n", 733 | "\u001B[1m> Finished chain.\u001B[0m\n", 734 | "LangChain is a framework for developing applications powered by language models. It enables developers to build data-aware and agentic applications that can call out to a language model, connect it to other sources of data, and allow it to interact with its environment. LangChain includes a variety of modules, use cases, reference documents, an ecosystem, and additional resources to support the development of these applications.\n", 735 | "\n", 736 | "\n", 737 | "Sources:\n", 738 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/0_Welcome_to_LangChain.txt\n", 739 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/1_Welcome_to_LangChain.txt\n", 740 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/1_Welcome_to_LangChain.txt\n", 741 | "/Users/erictak/PycharmProjects/freya/data/langchain_doc_small/0_Welcome_to_LangChain.txt\n" 742 | ] 743 | } 744 | ], 745 | "source": [ 746 | "query = \"What is Lanchain?\"\n", 747 | "llm_response = qa_chain(query)\n", 748 | "process_llm_response(llm_response)" 749 | ], 750 | "metadata": { 751 | "collapsed": false, 752 | "ExecuteTime": { 753 | "end_time": "2023-10-22T14:11:16.561630Z", 754 | "start_time": "2023-10-22T14:11:11.121119Z" 755 | } 756 | }, 757 | "id": "63197e0cca2ae5eb" 758 | }, 759 | { 760 | "cell_type": "code", 761 | "execution_count": null, 762 | "outputs": [], 763 | "source": [], 764 | "metadata": { 765 | "collapsed": false 766 | }, 767 | "id": "5cd522fb30e2c4d0" 768 | } 769 | ], 770 | "metadata": { 771 | "kernelspec": { 772 | "display_name": "Python 3", 773 | "language": "python", 774 | "name": "python3" 775 | }, 776 | "language_info": { 777 | "codemirror_mode": { 778 | "name": "ipython", 779 | "version": 2 780 | }, 781 | "file_extension": ".py", 782 | "mimetype": "text/x-python", 783 | "name": "python", 784 | "nbconvert_exporter": "python", 785 | "pygments_lexer": "ipython2", 786 | "version": "2.7.6" 787 | } 788 | }, 789 | "nbformat": 4, 790 | "nbformat_minor": 5 791 | } 792 | --------------------------------------------------------------------------------