{"id":4690,"date":"2023-06-19T22:15:03","date_gmt":"2023-06-19T21:15:03","guid":{"rendered":"https:\/\/www.utilewebsites.nl\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/"},"modified":"2023-06-19T22:27:51","modified_gmt":"2023-06-19T21:27:51","slug":"simple-script-to-use-chatgpt-on-your-own-files-with-langchain","status":"publish","type":"wz_knowledgebase","link":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/","title":{"rendered":"Simple script to use ChatGPT on your own files with LangChain"},"content":{"rendered":"\n<p><strong>Introduction:<\/strong> Large language models (LLMs) have revolutionized the development of applications by enabling powerful natural language processing capabilities. However, integrating LLMs with other sources of computation or knowledge can further enhance their functionality. This article introduces a simple script that leverages LangChain, a library designed to combine LLMs with various tools and utilities. Specifically, we demonstrate how to use ChatGPT, one of the LLMs, on your own text files to facilitate question answering and chatbot functionalities.<\/p>\n\n\n\n<p>Source : <a href=\"https:\/\/github.com\/techleadhd\/chatgpt-retrieval\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/techleadhd\/chatgpt-retrieval<\/a><\/p>\n\n\n\n<p><strong>Step-by-step guide:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Installation and configuration: Start by installing the LangChain library by running \"pip install langchain\" or \"conda install langchain -c conda-forge\" in your terminal.<\/li>\n\n\n\n<li>Importing libraries: Import the required libraries into your Python script. This includes \"os\", \"sys\", \"openai\" and several modules of the LangChain library, such as \"RetrievalQA\", \"ChatOpenAI\", \"DirectoryLoader\", \"TextLoader\", \"OpenAIEmbeddings\", \"VectorstoreIndexCreator\" and \"Chroma\".<\/li>\n\n\n\n<li>Setting the API key: Set your OpenAI API key by assigning it to the environment variable \"OPENAI_API_KEY\". Replace \"constants.APIKEY\" in the script with your actual API key.<\/li>\n\n\n\n<li>Loading and indexing data: Define the source of your data by creating a \"TextLoader\" or \"DirectoryLoader\" object, depending on whether you want to work with a single text file or a directory with multiple files. Adjust the loader accordingly and specify the file or folder.<\/li>\n\n\n\n<li>Creating the index: Create an index using \"VectorstoreIndexCreator\" by passing the loader as a parameter. If you want to cache and reuse the index, set \"PERSIST\" to True and specify a persistent directory. Otherwise, set \"PERSIST\" to False.<\/li>\n\n\n\n<li>Initializing the ChatGPT-LangChain Chain: Create an instance of a RetrievalQA-chain using \"ChatOpenAI\" as the language model (for example, model=\"gpt-3.5-turbo\"). Specify the retriever as the previously created index vector store using \"as_retriever(search_kwargs={\"k\": 1})\".<\/li>\n\n\n\n<li>Run searches: Specify a query as an argument to the script, which can be passed through the command line. For example, run the script with \"python script.py \"What is the capital of France?\"\". The script will print ChatGPT's response based on the query provided.<\/li>\n<\/ol>\n\n\n\n<p>Conclusion: By following the steps outlined in this article and utilizing the provided script, you can easily incorporate ChatGPT and LangChain to perform question answering and chatbot tasks on your own text files. The LangChain library offers additional functionalities and integrations, allowing you to explore various applications of large language models in your projects. Refer to the LangChain documentation for more details and advanced usage.<\/p>\n\n\n\n<p><strong>ChatGPT script with LangChain integration<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport sys\n\nimport openai\nfrom langchain.chains import RetrievalQA\nfrom langchain.chat_models import ChatOpenAI\nfrom langchain.document_loaders import DirectoryLoader, TextLoader\nfrom langchain.embeddings import OpenAIEmbeddings\nfrom langchain.indexes import VectorstoreIndexCreator\nfrom langchain.llms import OpenAI\nfrom langchain.vectorstores import Chroma\n\nimport constants\n\nos.environ&#91;\"OPENAI_API_KEY\"] = constants.APIKEY\n\n# Enable to cache &amp; reuse the model to disk (for repeated queries on the same data)\nPERSIST = False\n\nquery = sys.argv&#91;1]\n\nif PERSIST and os.path.exists(\"persist\"):\n  print(\"Reusing index...\\n\")\n  vectorstore = Chroma(persist_directory=\"persist\", embedding_function=OpenAIEmbeddings())\n  from langchain.indexes.vectorstore import VectorStoreIndexWrapper\n  index = VectorStoreIndexWrapper(vectorstore=vectorstore)\nelse:\n  loader = TextLoader('data.txt')\n  # This code can also import folders, including various filetypes like PDFs using the DirectoryLoader.\n  # loader = DirectoryLoader(\".\", glob=\"*.txt\")\n  if PERSIST:\n    index = VectorstoreIndexCreator(vectorstore_kwargs={\"persist_directory\":\"persist\"}).from_loaders(&#91;loader])\n  else:\n    index = VectorstoreIndexCreator().from_loaders(&#91;loader])\n\nchain = RetrievalQA.from_chain_type(\n  llm=ChatOpenAI(model=\"gpt-3.5-turbo\"),\n  retriever=index.vectorstore.as_retriever(search_kwargs={\"k\": 1}),\n)\nprint(chain.run(query))\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Large language models (LLMs) have revolutionized the development of applications by enabling powerful natural language processing capabilities. However, integrating LLMs with other sources of computation or knowledge can further enhance their functionality. This article introduces a simple script that leverages LangChain, a library designed to combine LLMs with various tools and utilities. Specifically, we demonstrate how to use ChatGPT, one of the LLMs, on your own text files to facilitate question answering and chatbot functionalities. Source : https:\/\/github.com\/techleadhd\/chatgpt-retrieval Step-by-step guide: Conclusion: By following the steps outlined in this article and utilizing the provided script, you can easily incorporate ChatGPT&nbsp;<a href=\"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/\" class=\"read-more\">Continue Reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"wzkb_category":[93],"wzkb_tag":[],"class_list":["post-4690","wz_knowledgebase","type-wz_knowledgebase","status-publish","hentry","wzkb_category-ai-artificial-intelligence"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple script to use ChatGPT on your own files with LangChain - Utilewebsites<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple script to use ChatGPT on your own files with LangChain - Utilewebsites\" \/>\n<meta property=\"og:description\" content=\"Introduction: Large language models (LLMs) have revolutionized the development of applications by enabling powerful natural language processing capabilities. However, integrating LLMs with other sources of computation or knowledge can further enhance their functionality. This article introduces a simple script that leverages LangChain, a library designed to combine LLMs with various tools and utilities. Specifically, we demonstrate how to use ChatGPT, one of the LLMs, on your own text files to facilitate question answering and chatbot functionalities. Source : https:\/\/github.com\/techleadhd\/chatgpt-retrieval Step-by-step guide: Conclusion: By following the steps outlined in this article and utilizing the provided script, you can easily incorporate ChatGPT&nbsp;Continue Reading\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/\" \/>\n<meta property=\"og:site_name\" content=\"Utilewebsites\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-19T21:27:51+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\\\/\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\\\/\",\"name\":\"Simple script to use ChatGPT on your own files with LangChain - Utilewebsites\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#website\"},\"datePublished\":\"2023-06-19T21:15:03+00:00\",\"dateModified\":\"2023-06-19T21:27:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowledge Base\",\"item\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Simple script to use ChatGPT on your own files with LangChain\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/\",\"name\":\"Utilewebsites\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#organization\",\"name\":\"Utilewebsites\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/logo-Utilewebsites-2017.png\",\"contentUrl\":\"https:\\\/\\\/www.utilewebsites.nl\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/logo-Utilewebsites-2017.png\",\"width\":3000,\"height\":593,\"caption\":\"Utilewebsites\"},\"image\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple script to use ChatGPT on your own files with LangChain - Utilewebsites","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/","og_locale":"en_US","og_type":"article","og_title":"Simple script to use ChatGPT on your own files with LangChain - Utilewebsites","og_description":"Introduction: Large language models (LLMs) have revolutionized the development of applications by enabling powerful natural language processing capabilities. However, integrating LLMs with other sources of computation or knowledge can further enhance their functionality. This article introduces a simple script that leverages LangChain, a library designed to combine LLMs with various tools and utilities. Specifically, we demonstrate how to use ChatGPT, one of the LLMs, on your own text files to facilitate question answering and chatbot functionalities. Source : https:\/\/github.com\/techleadhd\/chatgpt-retrieval Step-by-step guide: Conclusion: By following the steps outlined in this article and utilizing the provided script, you can easily incorporate ChatGPT&nbsp;Continue Reading","og_url":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/","og_site_name":"Utilewebsites","article_modified_time":"2023-06-19T21:27:51+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/","url":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/","name":"Simple script to use ChatGPT on your own files with LangChain - Utilewebsites","isPartOf":{"@id":"https:\/\/www.utilewebsites.nl\/en\/#website"},"datePublished":"2023-06-19T21:15:03+00:00","dateModified":"2023-06-19T21:27:51+00:00","breadcrumb":{"@id":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/simple-script-to-use-chatgpt-on-your-own-files-with-langchain\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.utilewebsites.nl\/en\/"},{"@type":"ListItem","position":2,"name":"Knowledge Base","item":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/"},{"@type":"ListItem","position":3,"name":"Simple script to use ChatGPT on your own files with LangChain"}]},{"@type":"WebSite","@id":"https:\/\/www.utilewebsites.nl\/en\/#website","url":"https:\/\/www.utilewebsites.nl\/en\/","name":"Utilewebsites","description":"","publisher":{"@id":"https:\/\/www.utilewebsites.nl\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.utilewebsites.nl\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.utilewebsites.nl\/en\/#organization","name":"Utilewebsites","url":"https:\/\/www.utilewebsites.nl\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.utilewebsites.nl\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.utilewebsites.nl\/wp-content\/uploads\/2019\/08\/logo-Utilewebsites-2017.png","contentUrl":"https:\/\/www.utilewebsites.nl\/wp-content\/uploads\/2019\/08\/logo-Utilewebsites-2017.png","width":3000,"height":593,"caption":"Utilewebsites"},"image":{"@id":"https:\/\/www.utilewebsites.nl\/en\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase\/4690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase"}],"about":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/types\/wz_knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/comments?post=4690"}],"version-history":[{"count":4,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase\/4690\/revisions"}],"predecessor-version":[{"id":4695,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase\/4690\/revisions\/4695"}],"wp:attachment":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/media?parent=4690"}],"wp:term":[{"taxonomy":"wzkb_category","embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wzkb_category?post=4690"},{"taxonomy":"wzkb_tag","embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wzkb_tag?post=4690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}