![](https://hackmd.io/_uploads/HyswNkxoh.png) ## Overview In our [previous tutorial](https://hackmd.io/@metamike/langchain-magick-pt1), we walked through how to get up and running with [LangChain](https://python.langchain.com/docs/get_started/introduction.html). In this installment, we'll run through a short example of how to create and implement a `Chain` in both LangChain and [Magick](https://magickml.com/) so you can see the difference in process for both. To test what we learn, we'll be creating a fictional name for a fantasy football team. ## LangChain - In the [last tutorial](https://hackmd.io/@metamike/langchain-magick-pt1), we ensured all requirements were installed and set our `OPENAI_API_KEY` variable. If you have not completed this, please do so before moving forward. - We're now going to create another cell. With the first cell selected, simply press `B` to add a new cell `B`elow. In this cell, we're going to import the OpenAI LLM, set a temperature value and query the LLM to get our team name. Temperature governs the randomness and thus the creativity of the responses, and since we're wanting to get pretty creative, I've set this at 0.85. My favorite NFL tea is the Atlanta Falcons, and I want my team name to relate to them. Once you've created your new cell, enter the following: ```python= from langchain.llms import OpenAI llm = OpenAI(temperature=0.85) name = llm("I'm making a fantasy football roster with fictional players. My favorite team is the Atlanta Falcons and I want the name to relate to this team. Suggest a humorous name for the team.") print(name) ``` - To run this cell, press `CTRL + Enter`. Below is the output I got after running the cell. After running the cell a few times, the best output I got was `Fantales of the Falconeer`. Its ok... I guess? Maybe we can make it better with some more creative prompting. I changed my name variable value to `"I'm making a fantasy football roster with fictional players. Suggest a hilariously witty and culturally relevant name for the team, pulling from hall-of-fame Atlanta Falcons player names and key moments in Atlanta Falcons history."` This is definitely not efficient from a token count perspective, but just for testing, it's fine. My result using the new query? `Vick to the Future`. Noice. - What's a team without its players? To extend this example, we are going to use a `PromptTemplate`. A prompt template refers to a reproducible way to generate a prompt. It contains a text string ("the template"), that can take in a set of parameters from the end user and generates a prompt. - For our prompt template, we are going to reproduce our first example using a prompt template rather than directly querying the LLM with a prompt. In this case, I want someone to be able to change the name of the team they have an affinity with to adjust the output. I am therefore going to change `Atlanta Falcons` to an input variable called `your_team`. We are then going to define `your_team` at the bottom of the code block. ```python= from langchain.prompts import PromptTemplate prompt_template_name = PromptTemplate( input_variables = ['your_team'], template = "I'm making a fantasy football roster with fictional players. Suggest a hilariously witty and culturally relevant name for the team, pulling from hall-of-fame {your_team} player names and key moments in {your_team} history." ) prompt_template_name.format(your_team="Atlanta Falcons") ``` - After running the cell, we get the below output: `"I'm making a fantasy football roster with fictional players. Suggest a hilariously witty and culturally relevant name for the team, pulling from hall-of-fame Atlanta Falcons player names and key moments in Atlanta Falcons history."` - Now that we have our prompt template, we need to use this as our LLM prompt. Create a new cell and enter the below: ```python= from langchain.chains import LLMChain chain = LLMChain(llm=llm, prompt=prompt_template_name) chain.run("Atlanta Falcons") ``` - Our result? `'\n\nThe Dirty Birds of Fire & Ice'` ## Magick So how do we implement all this in Magick? Luckily Magick provides an incredibly easy way to create this chain. If you're not on the waitlist, sign up on the [Magick homepage](https://www.magickml.com/) (and DM me on Twitter at @itsmetamike for fast-tracked access), and if you are in alpha, follow the steps below. - In your `Composer` window, right-click the viewport and add an `Event Destructure` node and a `Generate Text` node. The `Event Destructure` node will take the text input event and extract the `content` value from it. It will then pass this as the prompt to the `Generate Text` node. In this node, you can select your LLM of choice (we will be using `gpt-3.5-turbo`) and adjust the model parameters (again, setting our temperature to `0.85` like our LangChain example). ![](https://hackmd.io/_uploads/BytGLMxoh.png) - Now we can type in the playtest window and we can directly prompt GPT 3.5 in our spell. ![](https://hackmd.io/_uploads/ry41jMxih.png) - To add the chain mechanism, we simply need to add the `Text Template` node. This node acts very similarly to the `PromptTemplate` functionality of LangChain, but it can be used in a variety of different applications. With the Text Template node selected, add an input `Socket` called `your_team`. This is the same variable name we used in our LangChain example. In the text editor, add the text we used in the LangChain example. Instead of using single curly brackets, use double curly brackets to inject the `your_team` variable. Connect the `Event Destructure` `Content` output to the `your_team` input socket, and connect the `Prompt` output to the `Input` input socket of the `Generate Text` node. In the playtest, try it out with a couple of team names. ![](https://hackmd.io/_uploads/Sksd2zxsn.png) ## Conclusion This is just the beginning of what you can do with LangChain, and how the framework can be implemented within Magick. Magick makes it very simple to implement many of LangChain's core features with a few clicks of a button. In the next tutorial we'll be looking into how to create SimpleSequentialChains and SequentialChains in LangChain and how to easily implement them in Magick. If you haven’t already, [join the waitlist now](https://www.magickml.com/). ![](https://hackmd.io/_uploads/S1R2rG1sh.png) :::info *MetaMike is virtual world experience developer and has a passion for contributing to the education and enablement of the open metaverse.* Follow MetaMike [Website](https://itsmetamike.xyz/) | [Twitter](https://twitter.com/itsmetamike) | [LinkedIn](https://www.linkedin.com/in/itsmetamike/) *If you'd like to support me, my work shown above is available to mint or purchase [here](https://opensea.io/collection/metamike-editions).*