<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Jacob Lee - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Jacob Lee - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 19 May 2026 10:28:19 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/jacoblee93/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use LangChain to Build With LLMs – A Beginner's Guide ]]>
                </title>
                <description>
                    <![CDATA[ Large language models (LLMs) are incredibly powerful general reasoning tools that are useful in a wide range of situations.  But working with LLMs presents challenges that are different from building traditional software: Calls tend to be long-runni... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/beginners-guide-to-langchain/</link>
                <guid isPermaLink="false">66b8df4738ac321fcd1e98ec</guid>
                
                    <category>
                        <![CDATA[ AI ]]>
                    </category>
                
                    <category>
                        <![CDATA[ LLM&#39;s  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Jacob Lee ]]>
                </dc:creator>
                <pubDate>Thu, 11 Apr 2024 23:11:37 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/pexels-pixabay-417273--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Large language models (LLMs) are incredibly powerful general reasoning tools that are useful in a wide range of situations. </p>
<p>But working with LLMs presents challenges that are different from building traditional software:</p>
<ul>
<li>Calls tend to be long-running, and stream generated output as it becomes available.</li>
<li>Instead of structured input (something like JSON) with fixed parameters, they take unstructured and arbitrary natural language as input. They are capable of “understanding” subtleties of that language.</li>
<li>They are non-deterministic. You may get different outputs even with the same input.</li>
</ul>
<p><a target="_blank" href="https://langchain.com/">LangChain</a> is a popular framework for creating LLM-powered apps. It was built with these and other factors in mind, and provides a wide range of <a target="_blank" href="https://python.langchain.com/docs/integrations/platforms/">integrations</a> with closed-source model providers (like <a target="_blank" href="https://openai.com/">OpenAI</a>, <a target="_blank" href="https://www.anthropic.com/">Anthropic</a>, and <a target="_blank" href="https://gemini.google.com/">Google</a>), open source models, and other third-party components like vectorstores.</p>
<p>This article will walk through the fundamentals of building with LLMs and <a target="_blank" href="https://python.langchain.com">LangChain’s Python library</a>. The only requirement is basic familiarity with Python, – no machine learning experience needed!</p>
<p>You’ll learn about:</p>
<ul>
<li><a class="post-section-overview" href="#heading-project-setup">Basic project setup</a></li>
<li><a class="post-section-overview" href="#heading-first-steps">Using Chat Models and other fundamental LangChain components</a></li>
<li><a class="post-section-overview" href="#heading-chaining">Using LangChain Expression Language to create chains</a></li>
<li><a class="post-section-overview" href="#heading-streaming">Streaming output as soon as it is generated</a></li>
<li><a class="post-section-overview" href="#heading-how-to-guide-generation-with-context">Passing context to steer the model’s output (basic RAG concepts)</a></li>
<li><a class="post-section-overview" href="#heading-debugging">Debugging and tracing the internals of your chains</a></li>
</ul>
<p>Let’s dive in!</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>We recommend using a <a target="_blank" href="https://jupyter.org/">Jupyter notebook</a> to run the code in this tutorial since it provides a clean, interactive environment. See <a target="_blank" href="https://jupyter.org/install">this page</a> for instructions on setting it up locally, or <a target="_blank" href="https://colab.research.google.com/drive/1MsQNc7AMS3qY4Y94_ZN8ppWcNh0RgD-v?usp=sharing">check out this Google Colab notebook</a> for an in-browser experience.</p>
<p>The first thing you'll need to do is choose which Chat Model you want to use. If you've ever used an interface like ChatGPT before, the basic idea of a Chat Model will be familiar to you – the model takes messages as input, and returns messages as output. The difference is that we'll be doing it in code.</p>
<p>This guide defaults to <a target="_blank" href="https://python.langchain.com/docs/integrations/platforms/anthropic/">Anthropic</a> and their Claude 3 Chat Models, but LangChain also has a <a target="_blank" href="https://python.langchain.com/docs/integrations/chat/">wide range of other integrations</a> to choose from, including OpenAI models like GPT-4.</p>
<pre><code class="lang-bash">pip install langchain_core langchain_anthropic
</code></pre>
<p>If you’re working in a Jupyter notebook, you’ll need to prefix <code>pip</code> with a <code>%</code> symbol like this: <code>%pip install langchain_core langchain_anthropic</code>.</p>
<p>You’ll also need an Anthropic API key, which you can <a target="_blank" href="https://console.anthropic.com/">obtain here</a> from their console. Once you have it, set as an environment variable named <code>ANTHROPIC_API_KEY</code>:</p>
<pre><code class="lang-python">export ANTHROPIC_API_KEY=<span class="hljs-string">"..."</span>
</code></pre>
<p>You can also pass a key directly into the model if you prefer.</p>
<h2 id="heading-first-steps">First steps</h2>
<p>You can initialize your model like this:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> langchain_anthropic <span class="hljs-keyword">import</span> ChatAnthropic

chat_model = ChatAnthropic(
    model=<span class="hljs-string">"claude-3-sonnet-20240229"</span>,
    temperature=<span class="hljs-number">0</span>
)

<span class="hljs-comment"># If you prefer to pass your key explicitly</span>
<span class="hljs-comment"># chat_model = ChatAnthropic(</span>
<span class="hljs-comment">#   model="claude-3-sonnet-20240229",</span>
<span class="hljs-comment">#   temperature=0,</span>
<span class="hljs-comment">#   api_key="YOUR_ANTHROPIC_API_KEY"</span>
<span class="hljs-comment"># )</span>
</code></pre>
<p>The <code>model</code> parameter is a string that matches one of <a target="_blank" href="https://docs.anthropic.com/claude/docs/models-overview#model-comparison">Anthropic’s supported models</a>. At the time of writing, Claude 3 Sonnet strikes a good balance between speed, cost, and reasoning capability. </p>
<p><code>temperature</code> is a measure of the amount of randomness the model uses to generate responses. For consistency, in this tutorial, we set it to <code>0</code> but you can experiment with higher values for creative use cases.</p>
<p>Now, let’s try running it:</p>
<pre><code class="lang-python">chat_model.invoke(<span class="hljs-string">"Tell me a joke about bears!"</span>)
</code></pre>
<p>Here’s the output:</p>
<pre><code class="lang-shell">AIMessage(content="Here's a bear joke for you:\\n\\nWhy did the bear dissolve in water?\\nBecause it was a polar bear!")
</code></pre>
<p>You can see that the output is something called an <code>AIMessage</code>. This is because Chat Models use <a target="_blank" href="https://python.langchain.com/docs/modules/model_io/chat/message_types/">Chat Messages</a> as input and output.</p>
<p><strong>Note:</strong> You were able to pass a simple string as input in the previous example because LangChain accepts a few forms of convenience shorthand that it automatically converts to the proper format. In this case, a single string is turned into an array with a single <code>HumanMessage</code>.</p>
<p>LangChain also contains abstractions for pure text-completion LLMs, which are string input and string output. But at the time of writing, the chat-tuned variants have overtaken LLMs in popularity. For example, GPT-4 and Claude 3 are both Chat Models.</p>
<p>To illustrate what’s going on, you can call the above with a more explicit list of messages:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> langchain_core.messages <span class="hljs-keyword">import</span> HumanMessage

chat_model.invoke([
    HumanMessage(<span class="hljs-string">"Tell me a joke about bears!"</span>)
])
</code></pre>
<p>And you get a similar output:</p>
<pre><code class="lang-shell">AIMessage(content="Here's a bear joke for you:\\n\\nWhy did the bear bring a briefcase to work?\\nHe was a business bear!")
</code></pre>
<h3 id="heading-prompt-templates">Prompt Templates</h3>
<p>Models are useful on their own, but it’s often convenient to parameterize inputs so that you don’t repeat boilerplate. LangChain provides <strong><a target="_blank" href="https://python.langchain.com/docs/modules/model_io/prompts/">Prompt Templates</a></strong> for this purpose. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/04/prompt_and_model--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em>Prompt templates in LangChain</em></p>
<p>A simple example would be something like this:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> langchain_core.prompts <span class="hljs-keyword">import</span> ChatPromptTemplate

joke_prompt = ChatPromptTemplate.from_messages([
    (<span class="hljs-string">"system"</span>, <span class="hljs-string">"You are a world class comedian."</span>),
    (<span class="hljs-string">"human"</span>, <span class="hljs-string">"Tell me a joke about {topic}"</span>)
])
</code></pre>
<p>You can apply the templating using the same <code>.invoke()</code> method as with Chat Models:</p>
<pre><code class="lang-python">joke_prompt.invoke({<span class="hljs-string">"topic"</span>: <span class="hljs-string">"beets"</span>})
</code></pre>
<p>Here’s the result:</p>
<pre><code class="lang-shell">ChatPromptValue(messages=[
    SystemMessage(content='You are a world class comedian.'),
    HumanMessage(content='Tell me a joke about beets')
])
</code></pre>
<p>Let’s go over each step:</p>
<ul>
<li>You construct a prompt template consisting of templates for a <code>SystemMessage</code> and a <code>HumanMessage</code> using <code>from_messages</code>.</li>
<li>You can think of <code>SystemMessages</code> as meta-instructions that are not part of the current conversation, but purely guide input.</li>
<li>The prompt template contains <code>{topic}</code> in curly braces. This denotes a required parameter named <code>"topic"</code>.</li>
<li>You invoke the prompt template with a dict with a key named <code>"topic"</code> and a value <code>"beets"</code>.</li>
<li>The result contains the formatted messages.</li>
</ul>
<p>Next, you'll learn how to use this prompt template with your Chat Model.</p>
<h2 id="heading-chaining">Chaining</h2>
<p>You may have noticed that both the Prompt Template and Chat Model implement the <code>.invoke()</code> method. In LangChain terms, they are both instances of <strong><a target="_blank" href="https://python.langchain.com/docs/expression_language/interface/">Runnables</a>.</strong></p>
<p>You can compose Runnables into “chains” using the pipe (<code>|</code>) operator where you <code>.invoke()</code> the next step with the output of the previous one. Here’s an example:</p>
<pre><code class="lang-python">chain = joke_prompt | chat_model
</code></pre>
<p>The resulting <code>chain</code> is itself a Runnable and automatically implements <code>.invoke()</code> (as well as several other methods, as we’ll see later). This is the foundation of <a target="_blank" href="https://python.langchain.com/docs/expression_language/get_started/">LangChain Expression Language (LCEL)</a>.</p>
<p>Let’s invoke this new chain:</p>
<pre><code class="lang-python">chain.invoke({<span class="hljs-string">"topic"</span>: <span class="hljs-string">"beets"</span>})
</code></pre>
<p>The chain returns a joke whose topic is beets:</p>
<pre><code class="lang-shell">AIMessage(content="Here's a beet joke for you:\\n\\nWhy did the beet blush? Because it saw the salad dressing!")
</code></pre>
<p>Now, let’s say you want to work with just the raw string output of the message. LangChain has a component called an <strong><a target="_blank" href="https://python.langchain.com/docs/modules/model_io/output_parsers/">Output Parser</a></strong>, which, as the name implies, is responsible for parsing the output of a model into a more accessible format. Since composed chains are also Runnable, you can again use the pipe operator:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> langchain_core.output_parsers <span class="hljs-keyword">import</span> StrOutputParser

str_chain = chain | StrOutputParser()

<span class="hljs-comment"># Equivalent to:</span>
<span class="hljs-comment"># str_chain = joke_prompt | chat_model | StrOutputParser()</span>
</code></pre>
<p>Cool! Now let’s invoke it:</p>
<pre><code class="lang-python">str_chain.invoke({<span class="hljs-string">"topic"</span>: <span class="hljs-string">"beets"</span>})
</code></pre>
<p>And the result is now a string as we’d hoped:</p>
<pre><code class="lang-shell">"Here's a beet joke for you:\\n\\nWhy did the beet blush? Because it saw the salad dressing!"
</code></pre>
<p>You still pass <code>{"topic": "beets"}</code> as input to the new <code>str_chain</code> because the first Runnable in the sequence is still the Prompt Template you declared before.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/04/prompt_model_and_output_parser--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em>Prompt model and output parser</em></p>
<h2 id="heading-streaming">Streaming</h2>
<p>One of the biggest advantages to composing chains with LCEL is the streaming experience. </p>
<p>All Runnables implement the <code>.stream()</code>method (and <code>.astream()</code> if you’re working in async environments), including chains. This method returns a generator that will yield output as soon as it’s available, which allows us to get output as quickly as possible.</p>
<p>While every Runnable implements <code>.stream()</code>, not all of them support multiple chunks. For example, if you call <code>.stream()</code> on a Prompt Template, it will just yield a single chunk with the same output as <code>.invoke()</code>.</p>
<p>You can iterate over the output using <code>for ... in</code> syntax. Try it with the <code>str_chain</code> you just declared:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> str_chain.stream({<span class="hljs-string">"topic"</span>: <span class="hljs-string">"beets"</span>}):
    print(chunk, end=<span class="hljs-string">"|"</span>)
</code></pre>
<p>And you get multiple strings as output (chunks are separated by a <code>|</code> character in the print function):</p>
<pre><code class="lang-shell">Here|'s| a| b|eet| joke| for| you|:|

Why| did| the| b|eet| bl|ush|?| Because| it| saw| the| sal|ad| d|ressing|!|
</code></pre>
<p>Chains composed like <code>str_chain</code> will start streaming as early as possible, which in this case is the Chat Model in the chain. </p>
<p>Some Output Parsers (like the <code>StrOutputParser</code> used here) and many LCEL <a target="_blank" href="https://python.langchain.com/docs/expression_language/primitives/">Primitives</a> are able to process streamed chunks from previous steps as they are generated – essentially acting as transform streams or passthroughs – and do not disrupt streaming.</p>
<h2 id="heading-how-to-guide-generation-with-context">How to Guide Generation with Context</h2>
<p>LLMs are trained on large quantities of data and have some innate “knowledge” of various topics. Still, it’s common to pass the model private or more specific data as context when answering to glean useful information or insights. If you've heard the term "RAG", or "retrieval-augmented generation" before, this is the core principle behind it.</p>
<p>One of the simplest examples of this is telling the LLM what the current date is. Because LLMs are snapshots of when they are trained, they can’t natively determine the current time. Here’s an example:</p>
<pre><code class="lang-python">chat_model = ChatAnthropic(model_name=<span class="hljs-string">"claude-3-sonnet-20240229"</span>)

chat_model.invoke(<span class="hljs-string">"What is the current date?"</span>)
</code></pre>
<p>The response:</p>
<pre><code class="lang-shell">AIMessage(content="Unfortunately, I don't actually have a concept of the current date and time. As an AI assistant without an integrated calendar, I don't have a dynamic sense of the present date. I can provide you with today's date based on when I was given my training data, but that may not reflect the actual current date you're asking about.")
</code></pre>
<p>Now, let’s see what happens when you give the model the current date as context:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> date

prompt = ChatPromptTemplate.from_messages([
    (<span class="hljs-string">"system"</span>, <span class="hljs-string">'You know that the current date is "{current_date}".'</span>),
    (<span class="hljs-string">"human"</span>, <span class="hljs-string">"{question}"</span>)
])

chain = prompt | chat_model | StrOutputParser()

chain.invoke({
    <span class="hljs-string">"question"</span>: <span class="hljs-string">"What is the current date?"</span>,
    <span class="hljs-string">"current_date"</span>: date.today()
})
</code></pre>
<p>And you can see, the model generates the current date:</p>
<pre><code class="lang-shell">"The current date is 2024-04-05."
</code></pre>
<p>Nice! Now, let's take it a step further. Language models are trained on vast quantities of data, but they don't know everything. Here's what happens if you directly ask the Chat Model a very specific question about a local restaurant:</p>
<pre><code class="lang-python">chat_model.invoke(
    <span class="hljs-string">"What was the Old Ship Saloon's total revenue in Q1 2023?"</span>
 )
</code></pre>
<p>The model doesn't know the answer natively, or even know which of the many Old Ship Saloons in the world we may be talking about:</p>
<pre><code class="lang-shell">AIMessage(content="I'm sorry, I don't have any specific financial data about the Old Ship Saloon's revenue in Q1 2023. As an AI assistant without access to the saloon's internal records, I don't have information about their future projected revenues. I can only provide responses based on factual information that has been provided to me.")
</code></pre>
<p>However, if we can give the model more context, we can guide it to come up with a good answer:</p>
<pre><code class="lang-python">SOURCE = <span class="hljs-string">"""
Old Ship Saloon 2023 quarterly revenue numbers:
Q1: $174782.38
Q2: $467372.38
Q3: $474773.38
Q4: $389289.23
"""</span>

rag_prompt = ChatPromptTemplate.from_messages([
    (<span class="hljs-string">"system"</span>, <span class="hljs-string">'You are a helpful assistant. Use the following context when responding:\n\n{context}.'</span>),
    (<span class="hljs-string">"human"</span>, <span class="hljs-string">"{question}"</span>)
])

rag_chain = rag_prompt | chat_model | StrOutputParser()

rag_chain.invoke({
    <span class="hljs-string">"question"</span>: <span class="hljs-string">"What was the Old Ship Saloon's total revenue in Q1 2023?"</span>,
    <span class="hljs-string">"context"</span>: SOURCE
})
</code></pre>
<p>This time, here's the result:</p>
<pre><code class="lang-shell">"According to the provided context, the Old Ship Saloon's revenue in Q1 2023 was $174,782.38."
</code></pre>
<p>The result looks good! Note that augmenting generation with additional context is a very deep topic - in the real world, this would likely take the form of a longer financial document or portion of a document retrieved from some other data source. RAG is a powerful technique to answer questions over large quantities of information.</p>
<p>You can check out <a target="_blank" href="https://python.langchain.com/docs/use_cases/question_answering/">LangChain’s retrieval-augmented generation (RAG) docs</a> to learn more.</p>
<h2 id="heading-debugging">Debugging</h2>
<p>Because LLMs are non-deterministic, it becomes more and more important to see the internals of what’s going on as your chains get more complex.</p>
<p>LangChain has a <code>set_debug()</code> method that will return more granular logs of the chain internals: Let’s see it with the above example.</p>
<p>First, we'll need to install the main <code>langchain</code> package for the entrypoint to import the method:</p>
<pre><code class="lang-python">%pip install langchain
</code></pre>
<p>Then add this code:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> langchain.globals <span class="hljs-keyword">import</span> set_debug

set_debug(<span class="hljs-literal">True</span>)

<span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> date

prompt = ChatPromptTemplate.from_messages([
    (<span class="hljs-string">"system"</span>, <span class="hljs-string">'You know that the current date is "{current_date}".'</span>),
    (<span class="hljs-string">"human"</span>, <span class="hljs-string">"{question}"</span>)
])

chain = prompt | chat_model | StrOutputParser()

chain.invoke({
    <span class="hljs-string">"question"</span>: <span class="hljs-string">"What is the current date?"</span>,
    <span class="hljs-string">"current_date"</span>: date.today()
})
</code></pre>
<p>There’s a lot more information!</p>
<pre><code class="lang-shell">[chain/start] [1:chain:RunnableSequence] Entering Chain run with input:
[inputs]
[chain/start] [1:chain:RunnableSequence &gt; 2:prompt:ChatPromptTemplate] Entering Prompt run with input:
[inputs]
[chain/end] [1:chain:RunnableSequence &gt; 2:prompt:ChatPromptTemplate] [1ms] Exiting Prompt run with output:
[outputs]
[llm/start] [1:chain:RunnableSequence &gt; 3:llm:ChatAnthropic] Entering LLM run with input:
{
  "prompts": [
    "System: You know that the current date is \\"2024-04-05\\".\\nHuman: What is the current date?"
  ]
}
...
[chain/end] [1:chain:RunnableSequence] [885ms] Exiting Chain run with output:
{
  "output": "The current date you provided is 2024-04-05."
}
</code></pre>
<p>You can see <a target="_blank" href="https://python.langchain.com/docs/guides/development/debugging/">this guide</a> for more information on debugging.</p>
<p>You can also use the <code>astream_events()</code> <a target="_blank" href="https://python.langchain.com/docs/expression_language/streaming/#using-stream-events">method</a> to return this data. This is useful if you want to use intermediate steps in your application logic. Note that this is an <code>async</code> method, and requires an extra <code>version</code> flag since it’s still in beta:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Turn off debug mode for clarity</span>
set_debug(<span class="hljs-literal">False</span>)

stream = chain.astream_events({
    <span class="hljs-string">"question"</span>: <span class="hljs-string">"What is the current date?"</span>,
    <span class="hljs-string">"current_date"</span>: date.today()
}, version=<span class="hljs-string">"v1"</span>)

<span class="hljs-keyword">async</span> <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> stream:
    print(event)
    print(<span class="hljs-string">"-----"</span>)
</code></pre>
<pre><code class="lang-shell">{'event': 'on_chain_start', 'run_id': '90785a49-987e-46bf-99ea-d3748d314759', 'name': 'RunnableSequence', 'tags': [], 'metadata': {}, 'data': {'input': {'question': 'What is the current date?', 'current_date': datetime.date(2024, 4, 5)}}}
-----
{'event': 'on_prompt_start', 'name': 'ChatPromptTemplate', 'run_id': '54b1f604-6b2a-48eb-8b4e-c57a66b4c5da', 'tags': ['seq:step:1'], 'metadata': {}, 'data': {'input': {'question': 'What is the current date?', 'current_date': datetime.date(2024, 4, 5)}}}
-----
{'event': 'on_prompt_end', 'name': 'ChatPromptTemplate', 'run_id': '54b1f604-6b2a-48eb-8b4e-c57a66b4c5da', 'tags': ['seq:step:1'], 'metadata': {}, 'data': {'input': {'question': 'What is the current date?', 'current_date': datetime.date(2024, 4, 5)}, 'output': ChatPromptValue(messages=[SystemMessage(content='You know that the current date is "2024-04-05".'), HumanMessage(content='What is the current date?')])}
-----
{'event': 'on_chat_model_start', 'name': 'ChatAnthropic', 'run_id': 'f5caa4c6-1b51-49dd-b304-e9b8e176623a', 'tags': ['seq:step:2'], 'metadata': {}, 'data': {'input': {'messages': [[SystemMessage(content='You know that the current date is "2024-04-05".'), HumanMessage(content='What is the current date?')]]}}}
-----
...
{'event': 'on_chain_end', 'name': 'RunnableSequence', 'run_id': '90785a49-987e-46bf-99ea-d3748d314759', 'tags': [], 'metadata': {}, 'data': {'output': 'The current date is 2024-04-05.'}}
-----
</code></pre>
<p>Finally, you can use an external service like <a target="_blank" href="https://smith.langchain.com">LangSmith</a> to add tracing. Here’s an example:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Sign up at &lt;https://smith.langchain.com/&gt;</span>
<span class="hljs-comment"># Set environment variables</span>

<span class="hljs-comment"># import os</span>

<span class="hljs-comment"># os.environ["LANGCHAIN_TRACING_V2"] = "true"</span>
<span class="hljs-comment"># os.environ["LANGCHAIN_API_KEY"] = "YOUR_KEY"</span>
<span class="hljs-comment"># os.environ["LANGCHAIN_PROJECT"] = "YOUR_PROJECT"</span>

chain.invoke({
  <span class="hljs-string">"question"</span>: <span class="hljs-string">"What is the current date?"</span>,
  <span class="hljs-string">"current_date"</span>: date.today()
})
</code></pre>
<pre><code class="lang-shell">"The current date is 2024-04-05."
</code></pre>
<p>LangSmith will capture the internals at each step, giving you a result <a target="_blank" href="https://smith.langchain.com/public/628a15bb-45c8-4d39-987a-2896684a66c2/r">like this</a>.</p>
<p>You can also tweak prompts and rerun model calls in a playground. Due to the non-deterministic nature of LLMs, you can also tweak prompts and rerun model calls in a playground, as well as create datasets and test cases to evaluate changes to your app and catch regressions.</p>
<h2 id="heading-thank-you">Thank you!</h2>
<p>You’ve now learned the basics of:</p>
<ul>
<li>LangChain’s <a target="_blank" href="https://python.langchain.com/docs/modules/model_io/chat/">Chat Model</a>, <a target="_blank" href="https://python.langchain.com/docs/modules/model_io/prompts/">Prompt Template</a>, and <a target="_blank" href="https://python.langchain.com/docs/modules/model_io/output_parsers/">Output Parser</a> components</li>
<li>How to chain components together with streaming.</li>
<li>Using outside information to guide model generation.</li>
<li>How to debug the internals of your chains.</li>
</ul>
<p>And if you want to learn more about Retrieval-Augmented Generation (RAG), here's a <a target="_blank" href="https://www.youtube.com/watch?v=sVcwVQRHIc8">RAG from scratch course on freeCodeCamp's YouTube channel</a>.</p>
<p>Check out the following for some good resources to continue your generative AI journey:</p>
<ul>
<li><a target="_blank" href="https://python.langchain.com/">LangChain’s Python docs</a></li>
<li><a target="_blank" href="https://www.youtube.com/@LangChain">LangChain’s YouTube channel</a></li>
</ul>
<p>You can also follow LangChain on X (formerly Twitter) <a target="_blank" href="https://twitter.com/LangChainAI">@LangChainAI</a> for the latest news, or me <a target="_blank" href="https://x.com/hacubu/">@Hacubu</a>.</p>
<p>Happy prompting!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
