Post Data
{
  "title": "Langchain and Beyond",
  "date": "2023-09-25T16:03:11.308Z",
  "slug": "langchain-and-beyond",
  "author": {
    "name": "Lucas Steinmacher"
  },
  "content": "<p>So, I'm pretty big into the AI hype and enjoy learning new things, so I thought\nI'd give an update on what I've been working on and some things that I'm\ninterested in.</p>\n<h2>AI Hackathon</h2>\n<p>I recently competed in an AI hackathon put on by\n<a href=\"https://www.fixie.ai/\">Fixie AI</a>, in which my team built a travel assistant\nbased on the Fixie AI Corpus API and their generative AI.JSX. The Fixie\n<a href=\"https://docs.fixie.ai/api/corpus/fixie-corpus-api\">Corpus API</a> allows you to\ncreate a corpus of documents that are crawled and indexed for access by Fixie\nAgents via their Corpus API, cURL, or the dashboard.\n<a href=\"https://docs.ai-jsx.com/\">AI.JSX </a> is a framework for building AI applications\nusing JavaScript and JSX, designed to integrate seamlessly with React-based\nprojects.</p>\n<p>We trained our app on articles based on preset locations to provide up-to-date\ntravel recommendations from <a href=\"https://www.atlasobscura.com/\">Atlas Obscura</a> and <a href=\"https://seattle.eater.com/\">Eater</a>, utilizing the Google\nMaps API. The idea was to allow a user to select any location from the Google\nMaps API and receive up-to-date travel information based on the selected sites.\nWhat we ended up with was more of a proof of concept, in which we had\npre-selected cities and only a few articles uploaded since the scraping took\nsome time. But what do you expect from a 5-hour hackathon?\n<a href=\"https://travel-recommendation-engine.vercel.app/travel\">Link</a> to the live demo.</p>\n<h2>Langchain.js</h2>\n<p>Also, I’m excited to have finished the latest update to this site! It's a\ntrivial example of how to use Langchain.Js for translations. I set up an API in\nthe comment section and gave every user who logs in five tokens to play with the\ntranslation presets that I've set up. Under the \"Edit\" section of a comment,\nyou'll find the translation selector with four presets to help you edit your\ncontent: German, Spanish, Bruh, and the Intelligizer! <sup><a href=\"#user-content-fn-1\" id=\"user-content-fnref-1\" data-footnote-ref aria-describedby=\"footnote-label\">1</a></sup></p>\n<p>The comment translated using ChatGPT 3.5-turbo using a single call with no conversation and parsed to a string rather than streamed in.</p>\n<pre><code class=\"hljs language-ts\"><span class=\"hljs-keyword\">async</span> <span class=\"hljs-keyword\">function</span> <span class=\"hljs-title function_\">langchainCall</span>(<span class=\"hljs-params\">content: <span class=\"hljs-built_in\">string</span></span>): <span class=\"hljs-title class_\">Promise</span>&#x3C;<span class=\"hljs-built_in\">string</span>> {\n    <span class=\"hljs-keyword\">if</span> (!openAIApiKey) <span class=\"hljs-keyword\">throw</span> <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">Error</span>(<span class=\"hljs-string\">\"No OpenAI API key found in env\"</span>)\n\n    <span class=\"hljs-keyword\">const</span> promptTemplate = <span class=\"hljs-title class_\">PromptTemplate</span>.<span class=\"hljs-title function_\">fromTemplate</span>(\n        <span class=\"hljs-string\">\"You are a helpful assistant that translates a comment into another language or format as instructed. your reply must be 500 characters or less if the content is over 500 characters abbreviate it to just 500 characters. {input}\"</span>\n    )\n\n    <span class=\"hljs-keyword\">const</span> llm = <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">ChatOpenAI</span>({\n        <span class=\"hljs-attr\">modelName</span>: <span class=\"hljs-string\">\"gpt-3.5-turbo\"</span>,\n        openAIApiKey,\n        <span class=\"hljs-attr\">streaming</span>: <span class=\"hljs-literal\">true</span>,\n        <span class=\"hljs-attr\">callbackManager</span>: <span class=\"hljs-title class_\">CallbackManager</span>.<span class=\"hljs-title function_\">fromHandlers</span>({\n            <span class=\"hljs-attr\">handleLLMStart</span>: <span class=\"hljs-function\">() =></span> {\n                <span class=\"hljs-variable language_\">console</span>.<span class=\"hljs-title function_\">log</span>(<span class=\"hljs-string\">`handleLLMStart`</span>)\n            },\n            <span class=\"hljs-attr\">handleLLMError</span>: <span class=\"hljs-function\">(<span class=\"hljs-params\">err</span>) =></span> {\n                <span class=\"hljs-variable language_\">console</span>.<span class=\"hljs-title function_\">log</span>(<span class=\"hljs-string\">`handleLLMError`</span>, err)\n            },\n            <span class=\"hljs-attr\">handleLLMEnd</span>: <span class=\"hljs-function\">() =></span> {\n                <span class=\"hljs-variable language_\">console</span>.<span class=\"hljs-title function_\">log</span>(<span class=\"hljs-string\">`handleLLMEnd`</span>)\n            },\n        }),\n    })\n\n    <span class=\"hljs-keyword\">const</span> outputParser = <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">StringOutputParser</span>();\n    <span class=\"hljs-keyword\">const</span> chain = <span class=\"hljs-title class_\">RunnableSequence</span>.<span class=\"hljs-title function_\">from</span>([promptTemplate, llm, outputParser]);\n    <span class=\"hljs-keyword\">const</span> result = <span class=\"hljs-keyword\">await</span> chain.<span class=\"hljs-title function_\">invoke</span>({ <span class=\"hljs-attr\">input</span>: content });\n    <span class=\"hljs-keyword\">return</span> result;\n}\n</code></pre>\n<p>The first two are self-explanatory and simply translate your comment into German\nor Spanish. The third, \"Bruh,\" gives you a personality similar to a surfer guy\nwho constantly talks about his buddy Chad. Finally, there's the Intelligizer,\nwhich is supposed to be kind of a joke<sup><a href=\"#user-content-fn-2\" id=\"user-content-fnref-2\" data-footnote-ref aria-describedby=\"footnote-label\">2</a></sup>, which will read the content of the blog\npost and expand upon your comment's content!</p>\n<p><img src=\"/images/translation_final.gif\" alt=\"Translator in action!\"></p>\n<p>Currently, the edit/translation function only transforms and saves your comment\nand doesn't have the memory to remember the original content of your comment.\nBut don't worry, if you just want to try it out, feel free to delete it right\nafterward. That functionality will be coming soon. It was just fun implementing\na quick translator with Langchain, and I'm excited to implement more AI and use\nLLMs on my site. So stay tuned for more features to come!</p>\n<p>That's it for now! Feel free to leave a comment and try out the translation to\nsee how ChatGPT does against Google's translation!</p>\n<section data-footnotes class=\"footnotes\"><h2 class=\"sr-only\" id=\"footnote-label\">Footnotes</h2>\n<ol>\n<li id=\"user-content-fn-1\">\n<p>Results may vary. <a href=\"#user-content-fnref-1\" data-footnote-backref=\"\" aria-label=\"Back to reference 1\" class=\"data-footnote-backref\">↩</a></p>\n</li>\n<li id=\"user-content-fn-2\">\n<p>Contrary to popular belief, using a feature like this does not actually make you ANY more intelligent. Read more, learn, grow. I'm adding in this silly option of what a call like this could do, what it should do. <a href=\"#user-content-fnref-2\" data-footnote-backref=\"\" aria-label=\"Back to reference 2\" class=\"data-footnote-backref\">↩</a></p>\n</li>\n</ol>\n</section>"
}

Langchain and Beyond

2023-09-25

4 min read

Written by: Lucas Steinmacher

So, I'm pretty big into the AI hype and enjoy learning new things, so I thought I'd give an update on what I've been working on and some things that I'm interested in.

AI Hackathon

I recently competed in an AI hackathon put on by Fixie AI, in which my team built a travel assistant based on the Fixie AI Corpus API and their generative AI.JSX. The Fixie Corpus API allows you to create a corpus of documents that are crawled and indexed for access by Fixie Agents via their Corpus API, cURL, or the dashboard. AI.JSX is a framework for building AI applications using JavaScript and JSX, designed to integrate seamlessly with React-based projects.

We trained our app on articles based on preset locations to provide up-to-date travel recommendations from Atlas Obscura and Eater, utilizing the Google Maps API. The idea was to allow a user to select any location from the Google Maps API and receive up-to-date travel information based on the selected sites. What we ended up with was more of a proof of concept, in which we had pre-selected cities and only a few articles uploaded since the scraping took some time. But what do you expect from a 5-hour hackathon? Link to the live demo.

Langchain.js

Also, I’m excited to have finished the latest update to this site! It's a trivial example of how to use Langchain.Js for translations. I set up an API in the comment section and gave every user who logs in five tokens to play with the translation presets that I've set up. Under the "Edit" section of a comment, you'll find the translation selector with four presets to help you edit your content: German, Spanish, Bruh, and the Intelligizer! 1

The comment translated using ChatGPT 3.5-turbo using a single call with no conversation and parsed to a string rather than streamed in.

async function langchainCall(content: string): Promise<string> {
    if (!openAIApiKey) throw new Error("No OpenAI API key found in env")

    const promptTemplate = PromptTemplate.fromTemplate(
        "You are a helpful assistant that translates a comment into another language or format as instructed. your reply must be 500 characters or less if the content is over 500 characters abbreviate it to just 500 characters. {input}"
    )

    const llm = new ChatOpenAI({
        modelName: "gpt-3.5-turbo",
        openAIApiKey,
        streaming: true,
        callbackManager: CallbackManager.fromHandlers({
            handleLLMStart: () => {
                console.log(`handleLLMStart`)
            },
            handleLLMError: (err) => {
                console.log(`handleLLMError`, err)
            },
            handleLLMEnd: () => {
                console.log(`handleLLMEnd`)
            },
        }),
    })

    const outputParser = new StringOutputParser();
    const chain = RunnableSequence.from([promptTemplate, llm, outputParser]);
    const result = await chain.invoke({ input: content });
    return result;
}

The first two are self-explanatory and simply translate your comment into German or Spanish. The third, "Bruh," gives you a personality similar to a surfer guy who constantly talks about his buddy Chad. Finally, there's the Intelligizer, which is supposed to be kind of a joke2, which will read the content of the blog post and expand upon your comment's content!

Translator in action!

Currently, the edit/translation function only transforms and saves your comment and doesn't have the memory to remember the original content of your comment. But don't worry, if you just want to try it out, feel free to delete it right afterward. That functionality will be coming soon. It was just fun implementing a quick translator with Langchain, and I'm excited to implement more AI and use LLMs on my site. So stay tuned for more features to come!

That's it for now! Feel free to leave a comment and try out the translation to see how ChatGPT does against Google's translation!

Footnotes

  1. Results may vary.

  2. Contrary to popular belief, using a feature like this does not actually make you ANY more intelligent. Read more, learn, grow. I'm adding in this silly option of what a call like this could do, what it should do.

Back to blogs →

Have an opinion of what I said? Find a typo? Just want to be nice?
Feel free to leave a comment!

You must be logged in to leave a comment.

Comments