# 02. Runnable structure (graph) review

LCEL `runnable` After creating it, you will often want to check it to better understand what is going on.

This laptop covers some ways to do this.

Copy

```
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key information
load_dotenv()
```

Copy

```
 True 
```

Copy

```
# LangSmith Set up tracking. https://smith.langchain.com
# !pip install langchain-teddynote
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("LCEL-Advanced")
```

Copy

```
 Start tracking LangSmith. 
[Project name] 
LCEL-Advanced 
```

Copy

```
# !pip install -qU faiss-cpu tiktoken

# Installing libraries for drawing graphs
# !pip install -qU grandalf
```

Copy

```
from langchain.prompts import ChatPromptTemplate
from langchain.vectorstores import FAISS
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

vectorstore = FAISS.from_texts(
    # Create FAISS vector stores from text data.
    ["Teddy is an AI engineer who loves programming!"],
    embedding=OpenAIEmbeddings(),
)

# Create a retriever based on a vector store.
retriever = vectorstore.as_retriever()

template = """Answer the question based only on the following context:
{context}  

Question: {question}"""

prompt = ChatPromptTemplate.from_template(
    template
)  # Create a ChatPromptTemplate based on a template.

model = ChatOpenAI(model="gpt-4o-mini")  # Initialize the ChatOpenAI model.

# Create a chain.
chain = (
    # Specify the search context and question.
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt  # Generates a prompt.
    | model  # Run the language model.
    | StrOutputParser()  # Parse the output result into a string.
)
```

#### Check graph configuration <a href="#id-1" id="id-1"></a>

You can get a graph of runnable.

`chain.get_graph()` The method returns the chain's execution graph.

* This method returns a graph object representing the connection between each node and node in the chain.
* The node in the graph represents each step in the chain, and the edge represents the data flow between the steps.

Copy

```
# Get a node from a graph of chains.
chain.get_graph().nodes
```

Copy

```
 {'42e8cf39cdc04753b462eadf704b90dd': Node(id='42e8cf39cdc04753b462eadf704b90dd', name='Parallel  Input', data=  Input'>, metadata=None),'c2838bfce6a9401ba2807cb5cb178fc7': Node(id='c2838bfce6a9401ba2807cb5cb178fc7', name<  Output', data=  Output'>, metadata=None),'e67d152153c6484b76a3746f60e3463': Node(id='e67d152153c6484b76a3746  '144b3154fa'44912ada6a839e0467': Node (id='e744b3154fa44912n{context} \n\nQuestion: {question}')], metadata=None), '6c6d616c02bd4c81b43810b64f6fc': Node(id=6  , async_client=  , root_client=  , root_async_client=  , model_name='gpt-4o-mini', openai_api_key=SecretStr('********'), openai_proxy=','dc2909a51b59  , metadata=None)} 
```

Copy

```
# Get an edge from the graph of a chain.
chain.get_graph().edges
```

Copy

```
 [Edge ='b253fc2bd96d4501b3e7bfcd4538e968', target=aab4632cd59, data<G1 data=None, conditional=False), Edge(source='e71b4fd10a1641fdbdf193d26560aa5e', target='266d15455db4fc99b1f0102  361740ae4a444bb949ed995e03139ae', target='11880a33c9124dacb537c57e47d1f5c7', data=None, conditional=False), Edge 
```

#### Graph output <a href="#id-2" id="id-2"></a>

Outputing the graph allows you to express it in an easy-to-understand form.

Although the output results are not very easy to read, the output allows you to check the graph in a more understandable form.

Copy

```
# Prints the graph of the chain in ASCII format.
chain.get_graph().print_ascii()
```

Copy

```
           +---------------------------------+         
           | Parallel  Input |         
           +---------------------------------+         
                    ** **                
                 *** ***             
               ** **           
+----------------------+ +-------------+  
| VectorStoreRetriever | | Passthrough |  
+----------------------+ +-------------+  
                    ** **                
                      *** ***                  
                         ** **                     
           +----------------------------------+        
           | Parallel  Output |        
           +----------------------------------+        
                             *                         
                             *                         
                             *                         
                  +--------------------+               
                  | ChatPromptTemplate |               
                  +--------------------+               
                             *                         
                             *                         
                             *                         
                      +------------+                   
                      | ChatOpenAI |                   
                      +------------+                   
                             *                         
                             *                         
                             *                         
                   +-----------------+                 
                   | StrOutputParser |                 
                   +-----------------+                 
                             *                         
                             *                         
                             *                         
                +-----------------------+              
                | StrOutputParserOutput |              
                +-----------------------+              
```

#### Bring prompt <a href="#id-3" id="id-3"></a>

An important part of the chain is the prompt used.

`chain.get_prompts()` The method returns a list of prompt objects used in the chain.

Copy

```
chain.get_prompts()  # Get the prompt used in the chain.
```

Copy

```
 [ChatPromptTemplate (input_variables=['context','question'], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate>['context','question 
```
