{"id":5564,"date":"2026-03-03T17:17:00","date_gmt":"2026-03-03T16:17:00","guid":{"rendered":"https:\/\/www.howto-do.it\/de\/?p=5564"},"modified":"2026-03-03T18:17:19","modified_gmt":"2026-03-03T17:17:19","slug":"langchain-tutorial-2026","status":"publish","type":"post","link":"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/","title":{"rendered":"LangChain Tutorial: KI-Apps mit Python entwickeln 2026"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Inhalt<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69d4ab7bbcee5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69d4ab7bbcee5\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Was_ist_LangChain_und_warum_sollten_Sie_es_lernen\" >Was ist LangChain und warum sollten Sie es lernen?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Warum_LangChain\" >Warum LangChain?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Voraussetzungen_fuer_dieses_LangChain_Tutorial\" >Voraussetzungen f\u00fcr dieses LangChain Tutorial<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Geschaetzte_Bearbeitungszeit\" >Gesch\u00e4tzte Bearbeitungszeit<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_1_Installation_und_Setup\" >Schritt 1: Installation und Setup<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Virtuelle_Umgebung_erstellen\" >Virtuelle Umgebung erstellen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#LangChain_installieren\" >LangChain installieren<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Requirementstxt_erstellen\" >Requirements.txt erstellen<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_2_API-Keys_sicher_konfigurieren\" >Schritt 2: API-Keys sicher konfigurieren<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#env-Datei_erstellen\" >.env-Datei erstellen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Keys_laden_und_verwenden\" >Keys laden und verwenden<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#gitignore_anlegen\" >.gitignore anlegen<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_3_Erstes_LLM-Setup_%E2%80%93_%E2%80%9CHello_World%E2%80%9D\" >Schritt 3: Erstes LLM-Setup \u2013 &#8220;Hello World&#8221;<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Alternative_LLM-Provider\" >Alternative LLM-Provider<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_4_Prompt_Templates_%E2%80%93_Strukturierte_Eingaben\" >Schritt 4: Prompt Templates \u2013 Strukturierte Eingaben<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Einfaches_Template\" >Einfaches Template<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Chat-Prompt-Template\" >Chat-Prompt-Template<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_5_Chains_%E2%80%93_Verkettung_von_Operationen\" >Schritt 5: Chains \u2013 Verkettung von Operationen<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Einfache_LLMChain\" >Einfache LLMChain<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Sequential_Chain_%E2%80%93_Mehrstufige_Verarbeitung\" >Sequential Chain \u2013 Mehrstufige Verarbeitung<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_6_Document_Loading_%E2%80%93_Daten_einlesen\" >Schritt 6: Document Loading \u2013 Daten einlesen<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#PDF_laden\" >PDF laden<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Website_laden\" >Website laden<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Mehrere_Dateien_laden\" >Mehrere Dateien laden<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_7_RAG_mit_Vector_Stores_%E2%80%93_Semantische_Suche\" >Schritt 7: RAG mit Vector Stores \u2013 Semantische Suche<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Texte_in_Vektordatenbank_speichern\" >Texte in Vektordatenbank speichern<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Semantische_Suche_durchfuehren\" >Semantische Suche durchf\u00fchren<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#RAG-Chain_erstellen\" >RAG-Chain erstellen<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_8_Agents_mit_Tools_%E2%80%93_Autonome_KI\" >Schritt 8: Agents mit Tools \u2013 Autonome KI<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Tools_definieren\" >Tools definieren<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-31\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Agent_initialisieren\" >Agent initialisieren<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-32\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_9_Memory_%E2%80%93_Konversationsgeschichte\" >Schritt 9: Memory \u2013 Konversationsgeschichte<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-33\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Conversation_Buffer_Memory\" >Conversation Buffer Memory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-34\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Summary_Memory_%E2%80%93_Fuer_lange_Konversationen\" >Summary Memory \u2013 F\u00fcr lange Konversationen<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-35\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Schritt_10_Deployment_%E2%80%93_Production_Ready\" >Schritt 10: Deployment \u2013 Production Ready<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-36\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#FastAPI-Server_erstellen\" >FastAPI-Server erstellen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-37\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Docker-Container\" >Docker-Container<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-38\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Environment_Variables_fuer_Production\" >Environment Variables f\u00fcr Production<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-39\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Praxisprojekt_RAG-Chatbot_fuer_Dokumentation\" >Praxisprojekt: RAG-Chatbot f\u00fcr Dokumentation<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-40\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#1_Projekt-Struktur\" >1. Projekt-Struktur<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-41\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#2_Vollstaendiger_Code\" >2. Vollst\u00e4ndiger Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-42\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#3_Testen\" >3. Testen<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-43\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Fehlerbehebung_Haeufige_Probleme\" >Fehlerbehebung: H\u00e4ufige Probleme<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-44\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Problem_1_%E2%80%9CRateLimitError%E2%80%9D_von_OpenAI\" >Problem 1: &#8220;RateLimitError&#8221; von OpenAI<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-45\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Problem_2_Vektordatenbank_zu_gross\" >Problem 2: Vektordatenbank zu gro\u00df<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-46\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Problem_3_Memory_waechst_zu_stark\" >Problem 3: Memory w\u00e4chst zu stark<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-47\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Best_Practices_fuer_Production\" >Best Practices f\u00fcr Production<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-48\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#1_Caching_implementieren\" >1. Caching implementieren<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-49\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#2_Error_Handling\" >2. Error Handling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-50\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#3_Monitoring\" >3. Monitoring<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-51\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#4_Streaming_fuer_bessere_UX\" >4. Streaming f\u00fcr bessere UX<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-52\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Weiterfuehrende_Ressourcen\" >Weiterf\u00fchrende Ressourcen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-53\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Fazit_Ihr_Weg_zur_KI-App-Entwicklung_mit_LangChain\" >Fazit: Ihr Weg zur KI-App-Entwicklung mit LangChain<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-54\" href=\"https:\/\/www.howto-do.it\/de\/langchain-tutorial-2026\/#Naechste_Schritte\" >N\u00e4chste Schritte<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Was_ist_LangChain_und_warum_sollten_Sie_es_lernen\"><\/span>Was ist LangChain und warum sollten Sie es lernen?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>K\u00fcnstliche Intelligenz revolutioniert die Softwareentwicklung, und LangChain steht an vorderster Front dieser Revolution. Wenn Sie nach einem umfassenden <strong>LangChain Tutorial<\/strong> suchen, das Sie von den Grundlagen bis zur produktionsreifen KI-Anwendung f\u00fchrt, sind Sie hier genau richtig. Dieses <strong>LangChain Tutorial<\/strong> zeigt Ihnen Schritt f\u00fcr Schritt, wie Sie leistungsstarke KI-Apps mit Python entwickeln und dabei die F\u00e4higkeiten moderner Large Language Models (LLMs) voll aussch\u00f6pfen.<\/p>\n<p>LangChain ist ein Open-Source-Framework, das die Entwicklung von Anwendungen mit Large Language Models (LLMs) wie GPT-4, Claude oder Llama drastisch vereinfacht. Es bietet modulare Bausteine f\u00fcr Prompt-Management, Chains, Agents, Memory und Integration mit externen Datenquellen \u2013 alles, was Sie brauchen, um intelligente, kontextbewusste Anwendungen zu erstellen.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Warum_LangChain\"><\/span>Warum LangChain?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Modularit\u00e4t:<\/strong> Wiederverwendbare Komponenten f\u00fcr schnelle Entwicklung<\/li>\n<li><strong>LLM-Agnostisch:<\/strong> Unterst\u00fctzt OpenAI, Anthropic, Hugging Face, lokale Modelle und mehr<\/li>\n<li><strong>Production-Ready:<\/strong> Integrierte Fehlerbehandlung, Caching und Monitoring<\/li>\n<li><strong>Riesiges \u00d6kosystem:<\/strong> Hunderte von Integrationen (Datenbanken, APIs, Tools)<\/li>\n<li><strong>RAG-Unterst\u00fctzung:<\/strong> Retrieval-Augmented Generation out-of-the-box<\/li>\n<li><strong>Agents:<\/strong> Autonome KI-Systeme, die Tools nutzen und Probleme l\u00f6sen k\u00f6nnen<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Voraussetzungen_fuer_dieses_LangChain_Tutorial\"><\/span>Voraussetzungen f\u00fcr dieses LangChain Tutorial<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Um diesem Tutorial folgen zu k\u00f6nnen, ben\u00f6tigen Sie:<\/p>\n<ul>\n<li><strong>Python 3.9+:<\/strong> Installiert und lauff\u00e4hig<\/li>\n<li><strong>pip oder Poetry:<\/strong> Package Manager f\u00fcr Python<\/li>\n<li><strong>API-Keys:<\/strong> OpenAI, Anthropic oder andere LLM-Provider (kostenlose Testversionen verf\u00fcgbar)<\/li>\n<li><strong>Code-Editor:<\/strong> VS Code, PyCharm oder \u00e4hnliches<\/li>\n<li><strong>Grundkenntnisse Python:<\/strong> Funktionen, Klassen, async\/await<\/li>\n<li><strong>Optional:<\/strong> Git f\u00fcr Version Control<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Geschaetzte_Bearbeitungszeit\"><\/span>Gesch\u00e4tzte Bearbeitungszeit<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Schnelldurchlauf: 2-3 Stunden<\/li>\n<li>Mit Praxisprojekt: 6-8 Stunden<\/li>\n<li>Vollst\u00e4ndige Vertiefung: 2-3 Tage<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_1_Installation_und_Setup\"><\/span>Schritt 1: Installation und Setup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Beginnen wir mit einer sauberen Python-Umgebung:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Virtuelle_Umgebung_erstellen\"><\/span>Virtuelle Umgebung erstellen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/><\/div><\/td><td><div class=\"text codecolorer\"># Projekt-Verzeichnis erstellen<br \/>\nmkdir langchain-tutorial<br \/>\ncd langchain-tutorial<br \/>\n<br \/>\n# Virtuelle Umgebung erstellen<br \/>\npython3 -m venv venv<br \/>\n<br \/>\n# Aktivieren (Linux\/Mac)<br \/>\nsource venv\/bin\/activate<br \/>\n<br \/>\n# Aktivieren (Windows)<br \/>\nvenv\\Scripts\\activate<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"LangChain_installieren\"><\/span>LangChain installieren<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/><\/div><\/td><td><div class=\"text codecolorer\"># Basis-Installation<br \/>\npip install langchain<br \/>\n<br \/>\n# Mit OpenAI-Unterst\u00fctzung<br \/>\npip install langchain-openai<br \/>\n<br \/>\n# Mit Community-Integrationen<br \/>\npip install langchain-community<br \/>\n<br \/>\n# F\u00fcr Vektordatenbanken<br \/>\npip install chromadb<br \/>\n<br \/>\n# F\u00fcr Document Loading<br \/>\npip install pypdf unstructured<br \/>\n<br \/>\n# Alle abh\u00e4ngigkeiten auf einmal<br \/>\npip install langchain langchain-openai langchain-community chromadb pypdf python-dotenv<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Requirementstxt_erstellen\"><\/span>Requirements.txt erstellen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\">langchain==0.1.0<br \/>\nlangchain-openai==0.0.5<br \/>\nlangchain-community==0.0.16<br \/>\nchromadb==0.4.22<br \/>\npypdf==4.0.1<br \/>\npython-dotenv==1.0.0<br \/>\nopenai==1.10.0<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_2_API-Keys_sicher_konfigurieren\"><\/span>Schritt 2: API-Keys sicher konfigurieren<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sicherheit first! Speichern Sie API-Keys niemals direkt im Code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"env-Datei_erstellen\"><\/span>.env-Datei erstellen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/><\/div><\/td><td><div class=\"text codecolorer\"># .env<br \/>\nOPENAI_API_KEY=sk-proj-...<br \/>\nANTHROPIC_API_KEY=sk-ant-...<br \/>\nHUGGINGFACE_API_KEY=hf_...<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Keys_laden_und_verwenden\"><\/span>Keys laden und verwenden<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/><\/div><\/td><td><div class=\"text codecolorer\">from dotenv import load_dotenv<br \/>\nimport os<br \/>\n<br \/>\n# .env-Datei laden<br \/>\nload_dotenv()<br \/>\n<br \/>\n# Keys abrufen<br \/>\nopenai_key = os.getenv(&quot;OPENAI_API_KEY&quot;)<br \/>\n<br \/>\n# Sicherstellen, dass Key vorhanden ist<br \/>\nif not openai_key:<br \/>\n&nbsp; &nbsp; raise ValueError(&quot;OPENAI_API_KEY nicht gefunden!&quot;)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"gitignore_anlegen\"><\/span>.gitignore anlegen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\"># .gitignore<br \/>\n.env<br \/>\nvenv\/<br \/>\n__pycache__\/<br \/>\n*.pyc<br \/>\n.DS_Store<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_3_Erstes_LLM-Setup_%E2%80%93_%E2%80%9CHello_World%E2%80%9D\"><\/span>Schritt 3: Erstes LLM-Setup \u2013 &#8220;Hello World&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Lassen Sie uns Ihr erstes LLM-Programm schreiben:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain_openai import ChatOpenAI<br \/>\nfrom dotenv import load_dotenv<br \/>\n<br \/>\n# Environment laden<br \/>\nload_dotenv()<br \/>\n<br \/>\n# LLM initialisieren<br \/>\nllm = ChatOpenAI(<br \/>\n&nbsp; &nbsp; model=&quot;gpt-4&quot;,<br \/>\n&nbsp; &nbsp; temperature=0.7,<br \/>\n&nbsp; &nbsp; max_tokens=500<br \/>\n)<br \/>\n<br \/>\n# Erste Abfrage<br \/>\nresponse = llm.invoke(&quot;Erkl\u00e4re LangChain in einem Satz.&quot;)<br \/>\nprint(response.content)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Alternative_LLM-Provider\"><\/span>Alternative LLM-Provider<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/><\/div><\/td><td><div class=\"text codecolorer\"># Anthropic Claude<br \/>\nfrom langchain_anthropic import ChatAnthropic<br \/>\nllm = ChatAnthropic(model=&quot;claude-3-sonnet-20240229&quot;)<br \/>\n<br \/>\n# Hugging Face<br \/>\nfrom langchain_community.llms import HuggingFaceHub<br \/>\nllm = HuggingFaceHub(repo_id=&quot;google\/flan-t5-xxl&quot;)<br \/>\n<br \/>\n# Lokales Modell (Ollama)<br \/>\nfrom langchain_community.llms import Ollama<br \/>\nllm = Ollama(model=&quot;llama2&quot;)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_4_Prompt_Templates_%E2%80%93_Strukturierte_Eingaben\"><\/span>Schritt 4: Prompt Templates \u2013 Strukturierte Eingaben<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Prompt Templates erm\u00f6glichen wiederverwendbare, parametrisierte Prompts:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Einfaches_Template\"><\/span>Einfaches Template<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.prompts import PromptTemplate<br \/>\n<br \/>\n# Template definieren<br \/>\ntemplate = &quot;&quot;&quot;<br \/>\nDu bist ein hilfreicher Assistent f\u00fcr {fachgebiet}.<br \/>\nBeantworte die folgende Frage auf Deutsch:<br \/>\n<br \/>\nFrage: {frage}<br \/>\n<br \/>\nAntwort:<br \/>\n&quot;&quot;&quot;<br \/>\n<br \/>\nprompt = PromptTemplate(<br \/>\n&nbsp; &nbsp; input_variables=[&quot;fachgebiet&quot;, &quot;frage&quot;],<br \/>\n&nbsp; &nbsp; template=template<br \/>\n)<br \/>\n<br \/>\n# Template bef\u00fcllen<br \/>\nformatted_prompt = prompt.format(<br \/>\n&nbsp; &nbsp; fachgebiet=&quot;Python-Programmierung&quot;,<br \/>\n&nbsp; &nbsp; frage=&quot;Was sind Decorators?&quot;<br \/>\n)<br \/>\n<br \/>\n# An LLM senden<br \/>\nresponse = llm.invoke(formatted_prompt)<br \/>\nprint(response.content)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Chat-Prompt-Template\"><\/span>Chat-Prompt-Template<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.prompts import ChatPromptTemplate<br \/>\n<br \/>\nchat_prompt = ChatPromptTemplate.from_messages([<br \/>\n&nbsp; &nbsp; (&quot;system&quot;, &quot;Du bist ein Experte f\u00fcr {expertise}. Antworte pr\u00e4zise und hilfreich.&quot;),<br \/>\n&nbsp; &nbsp; (&quot;human&quot;, &quot;{user_input}&quot;)<br \/>\n])<br \/>\n<br \/>\n# Verwenden<br \/>\nmessages = chat_prompt.format_messages(<br \/>\n&nbsp; &nbsp; expertise=&quot;Machine Learning&quot;,<br \/>\n&nbsp; &nbsp; user_input=&quot;Erkl\u00e4re Overfitting.&quot;<br \/>\n)<br \/>\n<br \/>\nresponse = llm.invoke(messages)<br \/>\nprint(response.content)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_5_Chains_%E2%80%93_Verkettung_von_Operationen\"><\/span>Schritt 5: Chains \u2013 Verkettung von Operationen<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Chains kombinieren mehrere Schritte zu einem Workflow:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Einfache_LLMChain\"><\/span>Einfache LLMChain<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.chains import LLMChain<br \/>\nfrom langchain_openai import ChatOpenAI<br \/>\nfrom langchain.prompts import PromptTemplate<br \/>\n<br \/>\nllm = ChatOpenAI(model=&quot;gpt-4&quot;)<br \/>\n<br \/>\nprompt = PromptTemplate(<br \/>\n&nbsp; &nbsp; input_variables=[&quot;produkt&quot;],<br \/>\n&nbsp; &nbsp; template=&quot;Schreibe einen Marketing-Slogan f\u00fcr {produkt}.&quot;<br \/>\n)<br \/>\n<br \/>\nchain = LLMChain(llm=llm, prompt=prompt)<br \/>\n<br \/>\n# Chain ausf\u00fchren<br \/>\nresult = chain.run(produkt=&quot;KI-gest\u00fctzte Projektmanagement-Software&quot;)<br \/>\nprint(result)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Sequential_Chain_%E2%80%93_Mehrstufige_Verarbeitung\"><\/span>Sequential Chain \u2013 Mehrstufige Verarbeitung<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.chains import SimpleSequentialChain<br \/>\n<br \/>\n# Chain 1: Idee generieren<br \/>\nchain1 = LLMChain(<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; prompt=PromptTemplate(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; input_variables=[&quot;thema&quot;],<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; template=&quot;Generiere eine innovative Gesch\u00e4ftsidee f\u00fcr: {thema}&quot;<br \/>\n&nbsp; &nbsp; )<br \/>\n)<br \/>\n<br \/>\n# Chain 2: Idee bewerten<br \/>\nchain2 = LLMChain(<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; prompt=PromptTemplate(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; input_variables=[&quot;idee&quot;],<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; template=&quot;Bewerte diese Gesch\u00e4ftsidee auf Machbarkeit (1-10): {idee}&quot;<br \/>\n&nbsp; &nbsp; )<br \/>\n)<br \/>\n<br \/>\n# Chains verketten<br \/>\nsequential_chain = SimpleSequentialChain(chains=[chain1, chain2])<br \/>\n<br \/>\n# Ausf\u00fchren<br \/>\nresult = sequential_chain.run(&quot;Nachhaltige Mobilit\u00e4t&quot;)<br \/>\nprint(result)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_6_Document_Loading_%E2%80%93_Daten_einlesen\"><\/span>Schritt 6: Document Loading \u2013 Daten einlesen<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>LangChain kann Dokumente aus verschiedenen Quellen laden:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"PDF_laden\"><\/span>PDF laden<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain_community.document_loaders import PyPDFLoader<br \/>\n<br \/>\nloader = PyPDFLoader(&quot;dokument.pdf&quot;)<br \/>\npages = loader.load_and_split()<br \/>\n<br \/>\nprint(f&quot;Anzahl Seiten: {len(pages)}&quot;)<br \/>\nprint(f&quot;Erste Seite: {pages[0].page_content[:500]}&quot;)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Website_laden\"><\/span>Website laden<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain_community.document_loaders import WebBaseLoader<br \/>\n<br \/>\nloader = WebBaseLoader(&quot;https:\/\/beispiel.de\/artikel&quot;)<br \/>\ndocs = loader.load()<br \/>\n<br \/>\nprint(docs[0].page_content[:1000])<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Mehrere_Dateien_laden\"><\/span>Mehrere Dateien laden<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain_community.document_loaders import DirectoryLoader<br \/>\nfrom langchain_community.document_loaders import TextLoader<br \/>\n<br \/>\nloader = DirectoryLoader(<br \/>\n&nbsp; &nbsp; '.\/daten\/',<br \/>\n&nbsp; &nbsp; glob=&quot;**\/*.txt&quot;,<br \/>\n&nbsp; &nbsp; loader_cls=TextLoader<br \/>\n)<br \/>\n<br \/>\ndocuments = loader.load()<br \/>\nprint(f&quot;Geladene Dokumente: {len(documents)}&quot;)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_7_RAG_mit_Vector_Stores_%E2%80%93_Semantische_Suche\"><\/span>Schritt 7: RAG mit Vector Stores \u2013 Semantische Suche<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Retrieval-Augmented Generation (RAG) erm\u00f6glicht es, LLMs mit eigenen Daten zu erweitern:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Texte_in_Vektordatenbank_speichern\"><\/span>Texte in Vektordatenbank speichern<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain_community.vectorstores import Chroma<br \/>\nfrom langchain_openai import OpenAIEmbeddings<br \/>\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter<br \/>\n<br \/>\n# Dokumente in Chunks aufteilen<br \/>\ntext_splitter = RecursiveCharacterTextSplitter(<br \/>\n&nbsp; &nbsp; chunk_size=1000,<br \/>\n&nbsp; &nbsp; chunk_overlap=200<br \/>\n)<br \/>\n<br \/>\nchunks = text_splitter.split_documents(documents)<br \/>\n<br \/>\n# Embeddings erstellen<br \/>\nembeddings = OpenAIEmbeddings()<br \/>\n<br \/>\n# Vektordatenbank erstellen<br \/>\nvectorstore = Chroma.from_documents(<br \/>\n&nbsp; &nbsp; documents=chunks,<br \/>\n&nbsp; &nbsp; embedding=embeddings,<br \/>\n&nbsp; &nbsp; persist_directory=&quot;.\/chroma_db&quot;<br \/>\n)<br \/>\n<br \/>\nprint(f&quot;Chunks gespeichert: {len(chunks)}&quot;)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Semantische_Suche_durchfuehren\"><\/span>Semantische Suche durchf\u00fchren<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\"># \u00c4hnliche Dokumente finden<br \/>\nquery = &quot;Wie funktioniert Machine Learning?&quot;<br \/>\ndocs = vectorstore.similarity_search(query, k=3)<br \/>\n<br \/>\nfor i, doc in enumerate(docs):<br \/>\n&nbsp; &nbsp; print(f&quot;\\n--- Dokument {i+1} ---&quot;)<br \/>\n&nbsp; &nbsp; print(doc.page_content[:300])<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"RAG-Chain_erstellen\"><\/span>RAG-Chain erstellen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.chains import RetrievalQA<br \/>\n<br \/>\nqa_chain = RetrievalQA.from_chain_type(<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; chain_type=&quot;stuff&quot;,<br \/>\n&nbsp; &nbsp; retriever=vectorstore.as_retriever(search_kwargs={&quot;k&quot;: 3})<br \/>\n)<br \/>\n<br \/>\n# Frage stellen<br \/>\nantwort = qa_chain.run(&quot;Was sind die Hauptvorteile von LangChain?&quot;)<br \/>\nprint(antwort)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_8_Agents_mit_Tools_%E2%80%93_Autonome_KI\"><\/span>Schritt 8: Agents mit Tools \u2013 Autonome KI<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Agents k\u00f6nnen selbstst\u00e4ndig Tools nutzen, um Probleme zu l\u00f6sen:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Tools_definieren\"><\/span>Tools definieren<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.agents import Tool<br \/>\nfrom langchain_community.utilities import GoogleSearchAPIWrapper<br \/>\nimport datetime<br \/>\n<br \/>\n# Tool 1: Websuche<br \/>\nsearch = GoogleSearchAPIWrapper()<br \/>\nsearch_tool = Tool(<br \/>\n&nbsp; &nbsp; name=&quot;Websuche&quot;,<br \/>\n&nbsp; &nbsp; func=search.run,<br \/>\n&nbsp; &nbsp; description=&quot;N\u00fctzlich f\u00fcr aktuelle Informationen aus dem Internet.&quot;<br \/>\n)<br \/>\n<br \/>\n# Tool 2: Rechner<br \/>\ndef rechner(eingabe: str) -&gt; str:<br \/>\n&nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return str(eval(eingabe))<br \/>\n&nbsp; &nbsp; except:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return &quot;Fehler bei der Berechnung&quot;<br \/>\n<br \/>\nrechner_tool = Tool(<br \/>\n&nbsp; &nbsp; name=&quot;Rechner&quot;,<br \/>\n&nbsp; &nbsp; func=rechner,<br \/>\n&nbsp; &nbsp; description=&quot;F\u00fchrt mathematische Berechnungen durch. Eingabe: Python-Ausdruck&quot;<br \/>\n)<br \/>\n<br \/>\n# Tool 3: Datum<br \/>\ndef datum_tool(eingabe: str) -&gt; str:<br \/>\n&nbsp; &nbsp; return datetime.datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<br \/>\n<br \/>\ndatum = Tool(<br \/>\n&nbsp; &nbsp; name=&quot;Datum&quot;,<br \/>\n&nbsp; &nbsp; func=datum_tool,<br \/>\n&nbsp; &nbsp; description=&quot;Gibt das aktuelle Datum und Uhrzeit zur\u00fcck.&quot;<br \/>\n)<br \/>\n<br \/>\ntools = [search_tool, rechner_tool, datum]<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Agent_initialisieren\"><\/span>Agent initialisieren<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.agents import initialize_agent, AgentType<br \/>\n<br \/>\nagent = initialize_agent(<br \/>\n&nbsp; &nbsp; tools=tools,<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,<br \/>\n&nbsp; &nbsp; verbose=True &nbsp;# Zeigt Denkprozess<br \/>\n)<br \/>\n<br \/>\n# Agent ausf\u00fchren<br \/>\nantwort = agent.run(&quot;Wie sp\u00e4t ist es und was ist 1234 * 5678?&quot;)<br \/>\nprint(antwort)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_9_Memory_%E2%80%93_Konversationsgeschichte\"><\/span>Schritt 9: Memory \u2013 Konversationsgeschichte<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Memory erm\u00f6glicht es, den Kontext \u00fcber mehrere Interaktionen hinweg zu behalten:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Conversation_Buffer_Memory\"><\/span>Conversation Buffer Memory<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.memory import ConversationBufferMemory<br \/>\nfrom langchain.chains import ConversationChain<br \/>\n<br \/>\nmemory = ConversationBufferMemory()<br \/>\n<br \/>\nconversation = ConversationChain(<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; memory=memory,<br \/>\n&nbsp; &nbsp; verbose=True<br \/>\n)<br \/>\n<br \/>\n# Mehrere Nachrichten<br \/>\nprint(conversation.predict(input=&quot;Hallo, ich bin Max.&quot;))<br \/>\nprint(conversation.predict(input=&quot;Was ist mein Name?&quot;))<br \/>\nprint(conversation.predict(input=&quot;Erz\u00e4hl mir einen Witz.&quot;))<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Summary_Memory_%E2%80%93_Fuer_lange_Konversationen\"><\/span>Summary Memory \u2013 F\u00fcr lange Konversationen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.memory import ConversationSummaryMemory<br \/>\n<br \/>\nsummary_memory = ConversationSummaryMemory(llm=llm)<br \/>\n<br \/>\nconversation = ConversationChain(<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; memory=summary_memory,<br \/>\n&nbsp; &nbsp; verbose=True<br \/>\n)<br \/>\n<br \/>\n# L\u00e4ngere Konversation<br \/>\nfor i in range(5):<br \/>\n&nbsp; &nbsp; response = conversation.predict(input=f&quot;Nachricht {i+1}: Erz\u00e4hl mir etwas Interessantes.&quot;)<br \/>\n&nbsp; &nbsp; print(response)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Schritt_10_Deployment_%E2%80%93_Production_Ready\"><\/span>Schritt 10: Deployment \u2013 Production Ready<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"FastAPI-Server_erstellen\"><\/span>FastAPI-Server erstellen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/><\/div><\/td><td><div class=\"text codecolorer\">from fastapi import FastAPI<br \/>\nfrom pydantic import BaseModel<br \/>\nfrom langchain_openai import ChatOpenAI<br \/>\nfrom langchain.chains import ConversationChain<br \/>\nfrom langchain.memory import ConversationBufferMemory<br \/>\n<br \/>\napp = FastAPI()<br \/>\n<br \/>\n# LLM und Memory initialisieren<br \/>\nllm = ChatOpenAI(model=&quot;gpt-4&quot;)<br \/>\nmemory = ConversationBufferMemory()<br \/>\nconversation = ConversationChain(llm=llm, memory=memory)<br \/>\n<br \/>\nclass Query(BaseModel):<br \/>\n&nbsp; &nbsp; message: str<br \/>\n<br \/>\n@app.post(&quot;\/chat&quot;)<br \/>\nasync def chat(query: Query):<br \/>\n&nbsp; &nbsp; response = conversation.predict(input=query.message)<br \/>\n&nbsp; &nbsp; return {&quot;response&quot;: response}<br \/>\n<br \/>\n# Server starten: uvicorn main:app --reload<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Docker-Container\"><\/span>Docker-Container<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/><\/div><\/td><td><div class=\"text codecolorer\"># Dockerfile<br \/>\nFROM python:3.11-slim<br \/>\n<br \/>\nWORKDIR \/app<br \/>\n<br \/>\nCOPY requirements.txt .<br \/>\nRUN pip install --no-cache-dir -r requirements.txt<br \/>\n<br \/>\nCOPY . .<br \/>\n<br \/>\nCMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Environment_Variables_fuer_Production\"><\/span>Environment Variables f\u00fcr Production<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/><\/div><\/td><td><div class=\"text codecolorer\">import os<br \/>\nfrom langchain_openai import ChatOpenAI<br \/>\n<br \/>\nllm = ChatOpenAI(<br \/>\n&nbsp; &nbsp; model=os.getenv(&quot;LLM_MODEL&quot;, &quot;gpt-4&quot;),<br \/>\n&nbsp; &nbsp; temperature=float(os.getenv(&quot;LLM_TEMPERATURE&quot;, &quot;0.7&quot;)),<br \/>\n&nbsp; &nbsp; max_tokens=int(os.getenv(&quot;LLM_MAX_TOKENS&quot;, &quot;1000&quot;)),<br \/>\n&nbsp; &nbsp; request_timeout=int(os.getenv(&quot;LLM_TIMEOUT&quot;, &quot;30&quot;))<br \/>\n)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Praxisprojekt_RAG-Chatbot_fuer_Dokumentation\"><\/span>Praxisprojekt: RAG-Chatbot f\u00fcr Dokumentation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Jetzt bauen wir ein komplettes Projekt: Einen Chatbot, der Ihre Dokumentation kennt.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Projekt-Struktur\"><\/span>1. Projekt-Struktur<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\">rag-chatbot\/<br \/>\n\u251c\u2500\u2500 main.py<br \/>\n\u251c\u2500\u2500 requirements.txt<br \/>\n\u251c\u2500\u2500 .env<br \/>\n\u251c\u2500\u2500 data\/<br \/>\n\u2502 &nbsp; \u2514\u2500\u2500 docs\/ &nbsp;# Ihre Dokumentation hier<br \/>\n\u2514\u2500\u2500 chroma_db\/ &nbsp;# Wird automatisch erstellt<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"2_Vollstaendiger_Code\"><\/span>2. Vollst\u00e4ndiger Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/>89<br \/>90<br \/>91<br \/>92<br \/>93<br \/>94<br \/>95<br \/>96<br \/>97<br \/>98<br \/>99<br \/>100<br \/>101<br \/>102<br \/>103<br \/>104<br \/>105<br \/>106<br \/>107<br \/>108<br \/>109<br \/>110<br \/>111<br \/><\/div><\/td><td><div class=\"text codecolorer\">import os<br \/>\nfrom dotenv import load_dotenv<br \/>\nfrom langchain_openai import ChatOpenAI, OpenAIEmbeddings<br \/>\nfrom langchain_community.document_loaders import DirectoryLoader, TextLoader<br \/>\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter<br \/>\nfrom langchain_community.vectorstores import Chroma<br \/>\nfrom langchain.chains import ConversationalRetrievalChain<br \/>\nfrom langchain.memory import ConversationBufferMemory<br \/>\n<br \/>\nload_dotenv()<br \/>\n<br \/>\nclass RAGChatbot:<br \/>\n&nbsp; &nbsp; def __init__(self, docs_path=&quot;.\/data\/docs&quot;, db_path=&quot;.\/chroma_db&quot;):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.llm = ChatOpenAI(model=&quot;gpt-4&quot;, temperature=0.7)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.embeddings = OpenAIEmbeddings()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.docs_path = docs_path<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.db_path = db_path<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.vectorstore = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.chain = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; def load_documents(self):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;Dokumente laden und in Chunks aufteilen&quot;&quot;&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; loader = DirectoryLoader(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.docs_path,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glob=&quot;**\/*.txt&quot;,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader_cls=TextLoader<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; )<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; documents = loader.load()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; text_splitter = RecursiveCharacterTextSplitter(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunk_size=1000,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunk_overlap=200<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; )<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; chunks = text_splitter.split_documents(documents)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print(f&quot;&amp;#x2705; {len(documents)} Dokumente geladen, {len(chunks)} Chunks erstellt&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return chunks<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; def create_vectorstore(self, chunks):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;Vektordatenbank erstellen&quot;&quot;&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.vectorstore = Chroma.from_documents(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; documents=chunks,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; embedding=self.embeddings,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; persist_directory=self.db_path<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; )<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print(f&quot;&amp;#x2705; Vektordatenbank erstellt&quot;)<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; def setup_chain(self):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;RAG-Chain mit Memory konfigurieren&quot;&quot;&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; memory = ConversationBufferMemory(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memory_key=&quot;chat_history&quot;,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_messages=True,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_key=&quot;answer&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; )<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.chain = ConversationalRetrievalChain.from_llm(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llm=self.llm,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retriever=self.vectorstore.as_retriever(search_kwargs={&quot;k&quot;: 3}),<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memory=memory,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_source_documents=True<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; )<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print(&quot;&amp;#x2705; RAG-Chain konfiguriert&quot;)<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; def initialize(self):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;Komplettes Setup&quot;&quot;&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; # Pr\u00fcfen, ob DB existiert<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; if os.path.exists(self.db_path):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&quot;&amp;#x26a1; Lade bestehende Vektordatenbank...&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.vectorstore = Chroma(<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; persist_directory=self.db_path,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; embedding_function=self.embeddings<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&quot;&amp;#x1f4da; Erstelle neue Vektordatenbank...&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunks = self.load_documents()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.create_vectorstore(chunks)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.setup_chain()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print(&quot;&amp;#x1f680; Chatbot bereit!\\n&quot;)<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; def chat(self, question):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;Frage stellen&quot;&quot;&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; result = self.chain({&quot;question&quot;: question})<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; answer = result[&quot;answer&quot;]<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; sources = result.get(&quot;source_documents&quot;, [])<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print(f&quot;\\n&amp;#x1f916; Antwort: {answer}\\n&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; if sources:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&quot;&amp;#x1f4c4; Quellen:&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i, doc in enumerate(sources[:2], 1):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f&quot; &nbsp;{i}. {doc.metadata.get('source', 'Unbekannt')}&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return answer<br \/>\n<br \/>\n# Hauptprogramm<br \/>\nif __name__ == &quot;__main__&quot;:<br \/>\n&nbsp; &nbsp; chatbot = RAGChatbot()<br \/>\n&nbsp; &nbsp; chatbot.initialize()<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; # Interaktive Chat-Schleife<br \/>\n&nbsp; &nbsp; print(&quot;&amp;#x1f4ac; Stellen Sie Ihre Fragen (oder 'exit' zum Beenden):\\n&quot;)<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; while True:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; frage = input(&quot;Sie: &quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; if frage.lower() in [&quot;exit&quot;, &quot;quit&quot;, &quot;bye&quot;]:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&quot;&amp;#x1f44b; Auf Wiedersehen!&quot;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; if frage.strip():<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chatbot.chat(frage)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"3_Testen\"><\/span>3. Testen<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\"># Dokumentation in data\/docs\/ ablegen, dann:<br \/>\npython main.py<br \/>\n<br \/>\n# Beispiel-Fragen:<br \/>\n# - &quot;Wie installiere ich das System?&quot;<br \/>\n# - &quot;Welche Features gibt es?&quot;<br \/>\n# - &quot;Gibt es ein Tutorial?&quot;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Fehlerbehebung_Haeufige_Probleme\"><\/span>Fehlerbehebung: H\u00e4ufige Probleme<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Problem_1_%E2%80%9CRateLimitError%E2%80%9D_von_OpenAI\"><\/span>Problem 1: &#8220;RateLimitError&#8221; von OpenAI<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>L\u00f6sung:<\/strong> Retry-Logic implementieren:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain_openai import ChatOpenAI<br \/>\n<br \/>\nllm = ChatOpenAI(<br \/>\n&nbsp; &nbsp; model=&quot;gpt-4&quot;,<br \/>\n&nbsp; &nbsp; max_retries=3,<br \/>\n&nbsp; &nbsp; request_timeout=60<br \/>\n)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Problem_2_Vektordatenbank_zu_gross\"><\/span>Problem 2: Vektordatenbank zu gro\u00df<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>L\u00f6sung:<\/strong> Chunk-Size reduzieren oder Filter verwenden:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">retriever = vectorstore.as_retriever(<br \/>\n&nbsp; &nbsp; search_kwargs={<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;k&quot;: 3,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;filter&quot;: {&quot;source&quot;: &quot;important_docs&quot;}<br \/>\n&nbsp; &nbsp; }<br \/>\n)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Problem_3_Memory_waechst_zu_stark\"><\/span>Problem 3: Memory w\u00e4chst zu stark<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>L\u00f6sung:<\/strong> ConversationSummaryMemory oder Token-Limit:<\/p>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.memory import ConversationTokenBufferMemory<br \/>\n<br \/>\nmemory = ConversationTokenBufferMemory(<br \/>\n&nbsp; &nbsp; llm=llm,<br \/>\n&nbsp; &nbsp; max_token_limit=2000<br \/>\n)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Best_Practices_fuer_Production\"><\/span>Best Practices f\u00fcr Production<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"1_Caching_implementieren\"><\/span>1. Caching implementieren<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.cache import InMemoryCache<br \/>\nfrom langchain.globals import set_llm_cache<br \/>\n<br \/>\nset_llm_cache(InMemoryCache())<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"2_Error_Handling\"><\/span>2. Error Handling<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.schema import HumanMessage<br \/>\n<br \/>\ntry:<br \/>\n&nbsp; &nbsp; response = llm.invoke([HumanMessage(content=prompt)])<br \/>\nexcept Exception as e:<br \/>\n&nbsp; &nbsp; print(f&quot;LLM-Fehler: {e}&quot;)<br \/>\n&nbsp; &nbsp; # Fallback-Logik<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"3_Monitoring\"><\/span>3. Monitoring<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.callbacks import get_openai_callback<br \/>\n<br \/>\nwith get_openai_callback() as cb:<br \/>\n&nbsp; &nbsp; result = chain.run(query)<br \/>\n&nbsp; &nbsp; print(f&quot;Kosten: ${cb.total_cost}&quot;)<br \/>\n&nbsp; &nbsp; print(f&quot;Tokens: {cb.total_tokens}&quot;)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"4_Streaming_fuer_bessere_UX\"><\/span>4. Streaming f\u00fcr bessere UX<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler<br \/>\n<br \/>\nllm = ChatOpenAI(<br \/>\n&nbsp; &nbsp; streaming=True,<br \/>\n&nbsp; &nbsp; callbacks=[StreamingStdOutCallbackHandler()]<br \/>\n)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Weiterfuehrende_Ressourcen\"><\/span>Weiterf\u00fchrende Ressourcen<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>Offizielle Dokumentation:<\/strong> python.langchain.com<\/li>\n<li><strong>GitHub:<\/strong> github.com\/langchain-ai\/langchain<\/li>\n<li><strong>Discord Community:<\/strong> discord.gg\/langchain<\/li>\n<li><strong>Cookbook:<\/strong> github.com\/langchain-ai\/langchain\/tree\/master\/cookbook<\/li>\n<li><strong>YouTube-Kanal:<\/strong> LangChain Official<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Fazit_Ihr_Weg_zur_KI-App-Entwicklung_mit_LangChain\"><\/span>Fazit: Ihr Weg zur KI-App-Entwicklung mit LangChain<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Herzlichen Gl\u00fcckwunsch! Sie haben nun ein umfassendes <strong>LangChain Tutorial<\/strong> durchlaufen und alle wesentlichen Konzepte kennengelernt \u2013 von der Installation \u00fcber Prompt-Engineering und RAG bis hin zu autonomen Agents und Production Deployment.<\/p>\n<p>Die wichtigsten Erkenntnisse aus diesem <strong>LangChain Tutorial<\/strong>:<\/p>\n<ul>\n<li><strong>Modularit\u00e4t ist King:<\/strong> Nutzen Sie wiederverwendbare Komponenten (Chains, Tools, Memory)<\/li>\n<li><strong>RAG erweitert LLMs:<\/strong> Integrieren Sie eigene Daten f\u00fcr pr\u00e4zisere Antworten<\/li>\n<li><strong>Agents sind m\u00e4chtig:<\/strong> Lassen Sie KI selbstst\u00e4ndig Tools nutzen<\/li>\n<li><strong>Production braucht Planung:<\/strong> Error Handling, Monitoring, Caching sind essentiell<\/li>\n<li><strong>Sicherheit geht vor:<\/strong> API-Keys sch\u00fctzen, Input validieren<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Naechste_Schritte\"><\/span>N\u00e4chste Schritte<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol>\n<li><strong>Eigenes Projekt starten:<\/strong> Wenden Sie das Gelernte auf einen echten Use Case an<\/li>\n<li><strong>Community beitreten:<\/strong> Tauschen Sie sich mit anderen Entwicklern aus<\/li>\n<li><strong>Erweiterte Konzepte:<\/strong> LangSmith f\u00fcr Debugging, LangServe f\u00fcr Deployment<\/li>\n<li><strong>Alternative Modelle testen:<\/strong> Probieren Sie Claude, Llama, Mistral<\/li>\n<li><strong>Performance optimieren:<\/strong> Prompt-Engineering, Caching, Model-Selection<\/li>\n<\/ol>\n<p>LangChain entwickelt sich rasant weiter \u2013 bleiben Sie am Ball, experimentieren Sie und bauen Sie die KI-Anwendungen der Zukunft! Mit diesem Tutorial haben Sie das Fundament gelegt, um professionelle, skalierbare LLM-Applikationen zu entwickeln.<\/p>\n<p><strong>Haben Sie Fragen zu diesem LangChain Tutorial?<\/strong> Hinterlassen Sie einen Kommentar oder kontaktieren Sie unsere KI-Experten f\u00fcr individuelle Unterst\u00fctzung bei Ihrem Projekt!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vollst\u00e4ndiges LangChain Tutorial f\u00fcr Python-Entwickler. Lernen Sie Schritt f\u00fcr Schritt, wie Sie KI-Apps mit LLMs, RAG, Agents und Memory entwickeln. Von Installation bis Deployment.<\/p>\n","protected":false},"author":20,"featured_media":5562,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","rank_math_title":"LangChain Tutorial: KI-Apps mit Python entwickeln 2026","rank_math_description":"Vollst\u00e4ndiges LangChain Tutorial f\u00fcr Python-Entwickler. Lernen Sie Schritt f\u00fcr Schritt, wie Sie KI-Apps mit LLMs, RAG, Agents und Memory entwickeln. Von Installation bis Deployment.","rank_math_focus_keyword":"LangChain Tutorial","footnotes":""},"categories":[45],"tags":[],"class_list":["post-5564","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/posts\/5564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/comments?post=5564"}],"version-history":[{"count":1,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/posts\/5564\/revisions"}],"predecessor-version":[{"id":5566,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/posts\/5564\/revisions\/5566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/media\/5562"}],"wp:attachment":[{"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/media?parent=5564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/categories?post=5564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.howto-do.it\/de\/wp-json\/wp\/v2\/tags?post=5564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}