<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fábio Costa on Medium]]></title>
        <description><![CDATA[Stories by Fábio Costa on Medium]]></description>
        <link>https://medium.com/@fabioatc?source=rss-9d25c144eb87------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*x2tz953jH1e7OvENAbNnkQ.jpeg</url>
            <title>Stories by Fábio Costa on Medium</title>
            <link>https://medium.com/@fabioatc?source=rss-9d25c144eb87------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:20:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fabioatc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploy a FastAPI app to Deta Space]]></title>
            <link>https://medium.com/@fabioatc/deploy-a-fastapi-app-to-deta-space-cd4c73059a3e?source=rss-9d25c144eb87------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd4c73059a3e</guid>
            <category><![CDATA[deta]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Fábio Costa]]></dc:creator>
            <pubDate>Thu, 07 Sep 2023 14:19:24 GMT</pubDate>
            <atom:updated>2023-09-07T14:19:24.459Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes you need to host an API anywhere in the web because you had this crazy idea on doing one more todo app during your insomnia but then you realize Heroku is no longer free for this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4W8YGfiLftvWNLg4V7ESxw.png" /></figure><p>No worries, there are several free options in the web. This project aims to show you how to deploy a FastAPI app to Deta Space. You can go directly to the <a href="https://github.com/djquesadilla/fast-api-deploy-to-deta">GitHub Repo here</a>.</p><h3>Setting up Deta Space</h3><p>You should have the <a href="https://deta.space/docs/en/build/fundamentals/space-cli/">Space CLI</a> installed.</p><p>You should get the access the token from your Deta Space settings: space login</p><pre>space login</pre><p>Then we create our new project (I’m calling it <em>fast-api-sample</em> here):</p><pre>space new</pre><h3>The Spacefile</h3><p>Here’s where the shipping deploy magic happens. Your Spacefile should look similar to this:</p><pre>v: 0<br>app_name: &quot;FAPI SAMPLE&quot;<br>micros:<br>  - name: fastapi-sample<br>    src: ./<br>    engine: python3.9<br>    dev: .venv/bin/uvicorn main:app --reload</pre><p>Remember, main:app refers to main.py file / module and app.</p><h3>The FastAPI app</h3><p>This is the simplest FastAPI app you can get:</p><pre>from fastapi import FastAPI<br><br>app = FastAPI()<br>@app.get(&quot;/&quot;)<br>def read_root():<br>    return {&quot;Hello&quot;: &quot;World&quot;}</pre><p>Remember, you need the requirements.txt file with all the needed dependencies. In this case, the simpler is:</p><pre>fastapi</pre><h3>Deploying the app</h3><p>Now, you just need to push the app:</p><pre>space push</pre><p>And if everything is right, you should receive a happy message like this in the end:</p><pre>🎉  Successfully pushed your code and updated your Builder instance!<br>Builder instance: &lt;https://fastapisample-1-p5210071.deta.app&gt;</pre><p>And your API is up in the web ⛅.</p><p>🏗️Happy building!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd4c73059a3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Langchain and OpenAI functions to structure data]]></title>
            <link>https://medium.com/@fabioatc/using-langchain-and-openai-functions-to-structure-data-9bede2e5bb06?source=rss-9d25c144eb87------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bede2e5bb06</guid>
            <category><![CDATA[generative-ai]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Fábio Costa]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 10:20:01 GMT</pubDate>
            <atom:updated>2023-09-06T10:26:58.635Z</atom:updated>
            <content:encoded><![CDATA[<p>Working with LLMs is fun but sometimes too much free text is a pain to parse and process.</p><p>Fortunately, with Langchain and OpenAI functions we can structure data in a few lines of code.</p><p>Before OpenAI functions I was prompting stuff like “Please, give me the output in the following JSON structure” and praying to the gods that it would magically solve my problems.</p><p>Unfortunately I was being given replies with structured data prefixed with “Sure! Here’s your data:” or suffixed with “Hope this structure works for you. Enjoy!”.</p><h3>Getting structured outputs of given information</h3><p>We can use <strong>JsonSchema</strong> (Pydantic is awesome but it is still very buggy 🐛 for Langchain v0.0.281 at the time I wrote this article) to structure whatever we want. Let’s use the straightforward example from <a href="https://python.langchain.com/docs/modules/chains/how_to/openai_functions">Langchain documentation</a> to parse someone’s age:</p><pre>from typing import Optional<br><br>from dotenv import load_dotenv<br>from langchain.chains.openai_functions import create_structured_output_chain<br>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import ChatPromptTemplate<br><br>load_dotenv()  # take environment variables from .env so we can load OPENAI_API_KEY<br><br>json_schema = {<br>    &quot;title&quot;: &quot;Person&quot;,<br>    &quot;description&quot;: &quot;Identifying information about a person.&quot;,<br>    &quot;type&quot;: &quot;object&quot;,<br>    &quot;properties&quot;: {<br>        &quot;name&quot;: {&quot;title&quot;: &quot;Name&quot;, &quot;description&quot;: &quot;The person&#39;s name&quot;, &quot;type&quot;: &quot;string&quot;},<br>        &quot;age&quot;: {&quot;title&quot;: &quot;Age&quot;, &quot;description&quot;: &quot;The person&#39;s age&quot;, &quot;type&quot;: &quot;integer&quot;},<br>        &quot;fav_food&quot;: {<br>            &quot;title&quot;: &quot;Fav Food&quot;,<br>            &quot;description&quot;: &quot;The person&#39;s favorite food&quot;,<br>            &quot;type&quot;: &quot;string&quot;,<br>        },<br>    },<br>    &quot;required&quot;: [&quot;name&quot;, &quot;age&quot;],<br>}<br><br>llm = ChatOpenAI(model=&quot;gpt-4&quot;, temperature=0)<br>prompt = ChatPromptTemplate.from_messages(<br>    [<br>        (&quot;system&quot;, &quot;You are extracting information in structured formats.&quot;),<br>        (&quot;human&quot;, &quot;Use the given format to extract information from the following input: {input}&quot;)<br>    ]<br>)<br><br>chain = create_structured_output_chain(json_schema, llm, prompt, verbose=True)<br>output = chain.run(&quot;DJ Quesadilla is 38 and loves pizza&quot;)<br><br>print(output)</pre><h3>Getting structured outputs of generated information</h3><p>Sometimes we need to generate data on the fly and output it in the right format. Let’s take the previous example and generate a random list of <em>5 people with portuguese names</em> and their favourite food.</p><p>We just need to add a new people_json_schema to create an array of people and pass it to the create_structured_output_chain:</p><pre>from dotenv import load_dotenv<br>from langchain.chains.openai_functions import create_structured_output_chain<br>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import ChatPromptTemplate<br><br>load_dotenv()  # take environment variables from .env so we can load OPENAI_API_KEY<br><br>person_json_schema = {<br>    &quot;title&quot;: &quot;Person&quot;,<br>    &quot;description&quot;: &quot;Identifying information about a person.&quot;,<br>    &quot;type&quot;: &quot;object&quot;,<br>    &quot;properties&quot;: {<br>        &quot;name&quot;: {&quot;title&quot;: &quot;Name&quot;, &quot;description&quot;: &quot;The person&#39;s name&quot;, &quot;type&quot;: &quot;string&quot;},<br>        &quot;age&quot;: {&quot;title&quot;: &quot;Age&quot;, &quot;description&quot;: &quot;The person&#39;s age&quot;, &quot;type&quot;: &quot;integer&quot;},<br>        &quot;fav_food&quot;: {<br>            &quot;title&quot;: &quot;Fav Food&quot;,<br>            &quot;description&quot;: &quot;The person&#39;s favorite food&quot;,<br>            &quot;type&quot;: &quot;string&quot;,<br>        },<br>    },<br>    &quot;required&quot;: [&quot;name&quot;, &quot;age&quot;],<br>}<br><br>people_json_schema = {<br>    &quot;title&quot;: &quot;People&quot;,<br>    &quot;description&quot;: &quot;A list of people.&quot;,<br>    &quot;type&quot;: &quot;object&quot;,<br>    &quot;properties&quot;: {<br>        &quot;people&quot;: {<br>            &quot;title&quot;: &quot;People&quot;,<br>            &quot;description&quot;: &quot;A list of people&quot;,<br>            &quot;type&quot;: &quot;array&quot;,<br>            &quot;items&quot;: person_json_schema<br>        }<br>    }<br>}<br><br>llm = ChatOpenAI(model=&quot;gpt-4&quot;, temperature=0)<br>prompt = ChatPromptTemplate.from_messages(<br>    [<br>        (&quot;system&quot;, &quot;You are extracting information in structured formats.&quot;),<br>        (&quot;human&quot;, &quot;Use the given format to generate {number} random person with {type} names.&quot;)<br>    ]<br>)<br><br>chain = create_structured_output_chain(people_json_schema, llm, prompt, verbose=True)<br>output = chain.run({<br>    &quot;number&quot;: 5,<br>    &quot;type&quot;: &quot;portuguese&quot;<br>})<br><br>print(output)</pre><p>You can find the code in my <a href="https://github.com/djquesadilla/langchain-structured-data">github repo</a>.</p><p>Happy generating!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bede2e5bb06" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>