<?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[Use AI - Medium]]></title>
        <description><![CDATA[Use AI is a newsletter that provides practical guides on how to use AI, specifically LLMs, for individuals who are not programmers. Additionally, I discuss the implications and applications of AI in the real world, including no-code applications. - Medium]]></description>
        <link>https://medium.com/use-ai?source=rss----99315fefff4d---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Use AI - Medium</title>
            <link>https://medium.com/use-ai?source=rss----99315fefff4d---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 08:18:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/use-ai" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SQLCoder-2–7b: How to Reliably Query Data in Natural Language, on Consumer Hardware]]></title>
            <link>https://medium.com/use-ai/sqlcoder-2-7b-how-to-reliably-query-data-in-natural-language-on-consumer-hardware-cb352a3cf3ab?source=rss----99315fefff4d---4</link>
            <guid isPermaLink="false">https://medium.com/p/cb352a3cf3ab</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Sjoerd Tiemensma]]></dc:creator>
            <pubDate>Wed, 27 Mar 2024 16:14:17 GMT</pubDate>
            <atom:updated>2024-06-14T05:09:08.735Z</atom:updated>
            <content:encoded><![CDATA[<p>How we can leverage advances in AI to query our data locally using natural language, on your own PC.</p><p>Over the last few months, I’ve written multiple times about advances in both AI and the usability of AI. More and more is possible every day with really good, fine-tuned local models coming out, and I’d like to dive into an extremely practical and attainable example of that, which is querying your own data (CSV/Excel/SQL) on a system that’s a 100% local. Whether it’s Mac, Linux or Windows, with maybe 8 GB of RAM for decent performance.</p><p>At the beginning of February 2024 defog released <a href="https://huggingface.co/defog/sqlcoder-7b-2">SQLCoder-7b-2</a>. It’s a large language <strong>model fine-tuned to write SQL queries</strong>. 7b parameters mean it’s small enough to run on consumer hardware. No need to invest in extremely expensive GPUs or hundreds of gigabytes of RAM.</p><p>Next to that, software like LM Studio makes it easy to <strong>run local models and communicate with them</strong> on your own computer.</p><p>With the model and the ability to run the model, we need one last piece of the puzzle to combine it together and create an interface. In comes Streamlit, an <strong>open</strong>-<strong>source</strong> <strong>Python framework</strong> for machine learning and data science that works really well with Generative AI, allowing us to create a interface to query our Data. I’ll provide the code, so you can play around!</p><p>By the way, if you found this interesting and would like to learn more, discuss potential projects, or just connect, feel free to reach out to me on LinkedIn.</p><ul><li>Connect to me on <a href="https://www.linkedin.com/in/sjoerd-tiemensma-67aa703a/">LinkedIn</a></li><li>Check out my Webshop <a href="https://ko-fi.com/sjoti">here</a></li><li>Any clap/follow for this article helps me out!</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*KAtGKy366I4OYD9DaxBP_Q.gif" /><figcaption>Example of querying Olympics data in Real-Time on an RTX 3060</figcaption></figure><p>So the three pieces to this puzzle are:</p><ul><li>SQLCoder-2 7b — There’s also a 15b and a 70b model, if you have the hardware for it.</li><li><a href="https://lmstudio.ai/">LM Studio</a></li><li><a href="https://streamlit.io/">Streamlit</a></li></ul><blockquote>Important note:</blockquote><blockquote>SQLCoder-2 is built specifically for writing SQL queries. This model isn’t built to haveback and forth conversations. This means we can ask questions and have it write queries, and directly apply those to see the result.</blockquote><p>Let’s get started with getting the model up and running in LM Studio.</p><h4>Loading the model</h4><p>While LM Studio is easy to install and use, optimizing it can be a bit finnicky as it largely depends on your hardware and what it can or can’t handle. This is the same for the alternatives. It takes a bit of trial and error, and we’ll get into that.</p><p>Once you have LM Studio installed, search for SQLCoder. Select the 7b-2 version, and pick preferably the largest size quantization your PC can handle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qvcQplEdB16DW98GTcdvww.png" /></figure><p>The different versions from Q2 until Q8 are Quantized versions of the same model. Simply put, it’s like turning a high quality audio file into an mp3. Quantization significantly reduces the file size at the cost of a hit in quality. The lower the number is, the smaller the filesize gets but the worse the quality gets, too. Generally it’s good practice to steer away from models smaller than Q4.</p><p>Go to the server tab on the left, select the model in the big blue bar at the top. The model supports a large context length, but a large context length also means a lot of memory. We’re passing along the whole database schema to the model, which means we need some context length, but this takes up a lot of (V)RAM. 8000 tokens is a very safe bet, but you can go quite a bit lower. If you’re strapped for memory, try and see how small you can go before getting any errors when running in a bit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BwnVhXX-_du6cj6LxZqeDA.png" /></figure><p>Then there’s GPU offload. These models perform much faster when loaded onto VRAM instead of regular RAM. You can (partially) load the model onto VRAM by turning on GPU offload on the right under server model settings. The model has 35 layers in total, and you can play around with the amount of layers. It’s a bit of trial and error and completely depends on the amount of VRAM you have. Play with those numbers, see what happens. If you get an error, reduce the number. For comparison, here’s what happens on my system with zero offloading and with all layers offloaded on to my GPU:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvN_zwY-EK1gBm7QeXhG-w.gif" /><figcaption>Full offloading on the left vs no offloading on the right</figcaption></figure><p>As for the model, it performs the best when provided with the following prompt:</p><pre>### Task<br>Generate a SQL query to answer [QUESTION]{user_question}[/QUESTION]<br><br>### Database Schema<br>The query will run on a database with the following schema:<br>{table_metadata_string_DDL_statements}<br><br>### Answer<br>Given the database schema, here is the SQL query that [QUESTION]{user_question}[/QUESTION]<br>[SQL]</pre><p>This grounds the model in the types of SQL queries it will generate, and will be handled in the code properly so that you can simply select the database, and start chatting away.</p><p>As for Streamlit, if you have Python installed (if you don’t, this is a task that GPT-4 is very capable of helping you with) you can install it with pip alongside some other important packages that will be necessary.</p><p>Open your terminal and run the following code:</p><pre>pip install streamlit openai pandas</pre><p>Now we’re all set to get started with Streamlit!</p><h3>Streamlit Code</h3><p>We need both Streamlit code and a “database”. For ease of use, we’re using SQLite. Further below I’ve provided a script below to turn a csv into a .db file we can use with this app. But first, the Streamlit app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wcK4zsprnFCXsgPKRXVdpg.png" /></figure><p>The Streamlit app needs to do the following:</p><ul><li>Let the user select the database</li><li>Provide an interface to ask questions</li><li>Build the prompt</li><li>Communicate with LM Studio</li><li>Apply query to the database</li><li>Show results</li></ul><p>I’ve got everything ready for you to copy and paste.</p><pre>import os<br>import sqlite3<br>import streamlit as st<br>from openai import OpenAI<br>import pandas as pd<br><br>st.title(&#39;🔍 Query Your Data&#39;)<br><br># Assuming you&#39;ve set your OPENAI_API_KEY in your environment variables,<br># otherwise set it directly with openai.api_key = &quot;your-api-key&quot;<br>client = OpenAI(base_url=&quot;http://localhost:1234/v1&quot;, api_key=&quot;not-needed&quot;)<br><br># List all SQLite databases in the /databases/ directory<br>databases = [db for db in os.listdir(&#39;databases/&#39;) if db.endswith(&#39;.db&#39;)]<br><br># Create a dropdown menu in the sidebar for database selection<br>selected_db = st.sidebar.selectbox(&#39;Select a database:&#39;, databases)<br><br>def get_schema(db_path):<br>    &quot;&quot;&quot;Extract the schema (DDL) from an SQLite database.&quot;&quot;&quot;<br>    conn = sqlite3.connect(db_path)<br>    cursor = conn.cursor()<br>    cursor.execute(&quot;SELECT sql FROM sqlite_master WHERE type=&#39;table&#39;;&quot;)<br>    schema = &quot;\n&quot;.join([row[0] for row in cursor.fetchall()])<br>    cursor.close()<br>    conn.close()<br>    return schema<br><br>def adjust_sql_for_sqlite(sql_query):<br>    &quot;&quot;&quot;Adjusts an SQL query to be more compatible with SQLite.&quot;&quot;&quot;<br>    adjusted_query = sql_query.replace(&quot;ILIKE&quot;, &quot;LIKE&quot;)<br>    adjusted_query = adjusted_query.replace(&quot;TRUE&quot;, &quot;1&quot;).replace(&quot;FALSE&quot;, &quot;0&quot;)<br>    # Add more adjustments here as needed<br>    return adjusted_query<br><br>def generate_response(input_text, selected_db):<br>    db_schema = get_schema(f&#39;databases/{selected_db}&#39;)<br>    <br>    prompt = f&quot;&quot;&quot;<br>### Task<br>Generate a SQL query to answer [QUESTION]{input_text}[/QUESTION]<br><br>### Database Schema<br>The query will run on a database with the following schema:<br>{db_schema}<br><br>### Answer<br>Given the database schema, here is the SQL query that [QUESTION]{input_text}[/QUESTION]<br>[SQL]<br><br>&quot;&quot;&quot;<br>    <br>    response = client.chat.completions.create(<br>        model=&quot;sql&quot;,<br>        messages=[<br>            {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt}<br>        ],<br>        temperature=0.3,<br>        stream=True,<br>    )<br><br>    expander = st.expander(&quot;SQL Query&quot;, expanded=True)<br>    with expander:<br>        stream = st.write_stream(response)<br>    sql_query = stream<br><br>    # Adjust the SQL query for SQLite compatibility<br>    adjusted_sql_query = adjust_sql_for_sqlite(sql_query)<br>    <br>    # Execute the adjusted SQL query and return the result<br>    conn = sqlite3.connect(f&#39;databases/{selected_db}&#39;)<br>    cursor = conn.cursor()<br>    try:<br>        cursor.execute(adjusted_sql_query)  # Use the adjusted SQL query here<br>        rows = cursor.fetchall()<br>        <br>        # Convert the query results into a DataFrame for display below the expander<br>        if rows:<br>            columns = [description[0] for description in cursor.description]<br>            df = pd.DataFrame(rows, columns=columns)<br>            st.write(df)  # Display the DataFrame below the expander<br>        else:<br>            st.info(&quot;Query executed successfully, but no data was returned.&quot;)<br>    except Exception as e:<br>        st.error(f&quot;Error executing SQL query: {e}&quot;)<br>    finally:<br>        cursor.close()<br>        conn.close()<br><br><br>with st.form(&#39;my_form&#39;):<br>    text = st.text_area(&#39;Enter text:&#39;, &#39;Who won the most gold medals in 2012?&#39;)<br>    submitted = st.form_submit_button(&#39;Submit&#39;)<br>    if submitted:<br>        generate_response(text, selected_db)</pre><p>Save this as app.py. You can run it by opening a terminal in the same folder as app.py is located, and run the following command:</p><pre>streamlit run app.py</pre><p>This is how it all works:<br>Once you click the Submit button, the <strong>generate_response </strong>function will get to work. The <strong>get_schema(db_path) </strong>function will be called and automatically grab the database schema that is to be inserted in the prompt. It combines it with the template, the question asked and sends it over to LM studio to handle.</p><p>At that point, LM Studio will get to work and through the extremely new st.write_stream in Streamlit, you will see the query being generated in real time. Once a query is generated, it’ll execute it on the selected database, and show the end result in a dataframe.</p><h4>Database Setup</h4><p>As mentioned earlier, here’s<strong> a little script you can run to turn a csv file into a .db file.</strong></p><pre>import pandas as pd<br>import sqlite3<br>import os<br><br># Step 1: Load the CSV data into a pandas DataFrame<br>csv_file = &#39;Summer_Olympic_medals.csv&#39;<br>df = pd.read_csv(csv_file)<br><br># Extract the base name of the file without the extension<br>base_name = os.path.splitext(os.path.basename(csv_file))[0]<br><br># Step 2: Create a connection to a SQLite database<br>db_file = base_name + &#39;.db&#39;<br>conn = sqlite3.connect(db_file)<br><br># Step 3: Write the DataFrame to the SQLite database<br>df.to_sql(base_name, conn, if_exists=&#39;replace&#39;, index=False)<br><br># Don&#39;t forget to close the connection<br>conn.close()</pre><p>Put this script in the same folder as the csv file, adjust the file name and run by typing <em>Python tosqlite.py</em>. This will create a db file. Put the newly created .db file in the databases folder and it’s all set! Now you can ask queries and see the direct result!</p><h3>A few important notes</h3><p>While this is exciting and the model is VERY capable for its size, I want to mention a few important notes.</p><ul><li>The model CAN execute destructive queries that could be difficult to roll back. If you want to use this in more important settings, make sure it has the right permissions.</li><li>We’re using SQLite here for demonstration purposes, however, the model is fine-tuned to work with regular SQL and there’s are small differences between the two. For the sake of the demonstration, I’ve added a small extra function in the code that replaces two simple but common differences to prevent errors.</li><li>If you want to actually chat, you’d have to combine it with a different model.</li><li>The 7b model does a good job of writing queries, but often requires specific instructions. In the Olympics example (<a href="https://www.kaggle.com/datasets/ramontanoeiro/summer-olympic-medals-1986-2020">found on Kaggle</a>) it sometimes mixes up Host country and Country name. This can be solved by being more specific in your queries, and the larger models (15b and 70b one) are much better at inferring column names.</li></ul><p>I’ve got everything on my Github, with a more extensive version that adds a little bit of extra functionality, if you want to take a look!</p><p><a href="https://github.com/Sjotie/ChatWithSQL">https://github.com/Sjotie/ChatWithSQL</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb352a3cf3ab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/use-ai/sqlcoder-2-7b-how-to-reliably-query-data-in-natural-language-on-consumer-hardware-cb352a3cf3ab">SQLCoder-2–7b: How to Reliably Query Data in Natural Language, on Consumer Hardware</a> was originally published in <a href="https://medium.com/use-ai">Use AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Extract information automatically: No-code plus ChatGPT workflow]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/use-ai/set-forget-automate-no-code-plus-ai-for-smart-workflows-d7caaa357d38?source=rss----99315fefff4d---4"><img src="https://cdn-images-1.medium.com/max/953/1*sVrIz9Ulx0PIzWMWmUszLQ.gif" width="953"></a></p><p class="medium-feed-snippet">Complete guide to a workflow that allows you to automatically extract any information from any sort of document using Make.com and ChatGPT.</p><p class="medium-feed-link"><a href="https://medium.com/use-ai/set-forget-automate-no-code-plus-ai-for-smart-workflows-d7caaa357d38?source=rss----99315fefff4d---4">Continue reading on Use AI »</a></p></div>]]></description>
            <link>https://medium.com/use-ai/set-forget-automate-no-code-plus-ai-for-smart-workflows-d7caaa357d38?source=rss----99315fefff4d---4</link>
            <guid isPermaLink="false">https://medium.com/p/d7caaa357d38</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Sjoerd Tiemensma]]></dc:creator>
            <pubDate>Wed, 20 Mar 2024 13:28:53 GMT</pubDate>
            <atom:updated>2024-06-14T05:08:46.927Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Building a Custom AI Assistant using No-Code Make.com and the Assistants API]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/use-ai/building-a-custom-ai-assistant-using-no-code-make-com-and-the-assistants-api-32660c4aeee5?source=rss----99315fefff4d---4"><img src="https://cdn-images-1.medium.com/max/2600/1*I6yzdT3jOwrcbO8J2LEylg.gif" width="3553"></a></p><p class="medium-feed-snippet">Visualize how OpenAI&#x2019;s Assistants API works, and hook it up to any API.</p><p class="medium-feed-link"><a href="https://medium.com/use-ai/building-a-custom-ai-assistant-using-no-code-make-com-and-the-assistants-api-32660c4aeee5?source=rss----99315fefff4d---4">Continue reading on Use AI »</a></p></div>]]></description>
            <link>https://medium.com/use-ai/building-a-custom-ai-assistant-using-no-code-make-com-and-the-assistants-api-32660c4aeee5?source=rss----99315fefff4d---4</link>
            <guid isPermaLink="false">https://medium.com/p/32660c4aeee5</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[no-code]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Sjoerd Tiemensma]]></dc:creator>
            <pubDate>Wed, 21 Feb 2024 14:39:56 GMT</pubDate>
            <atom:updated>2024-06-14T05:11:18.714Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[ChatGPT Update: Making the Most of Mentioning Custom GPT’s]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/use-ai/chatgpt-update-making-the-most-of-mentioning-custom-gpts-f3abfe3b0dc6?source=rss----99315fefff4d---4"><img src="https://cdn-images-1.medium.com/max/1024/1*y-rz6q65j3KJB8yp_PZz9w.jpeg" width="1024"></a></p><p class="medium-feed-snippet">ChatGPT just added a new update, the ability to mention GPT&#x2019;s. It&#x2019;s a great new addition into making Custom GPT&#x2019;s abilities more and more&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/use-ai/chatgpt-update-making-the-most-of-mentioning-custom-gpts-f3abfe3b0dc6?source=rss----99315fefff4d---4">Continue reading on Use AI »</a></p></div>]]></description>
            <link>https://medium.com/use-ai/chatgpt-update-making-the-most-of-mentioning-custom-gpts-f3abfe3b0dc6?source=rss----99315fefff4d---4</link>
            <guid isPermaLink="false">https://medium.com/p/f3abfe3b0dc6</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Sjoerd Tiemensma]]></dc:creator>
            <pubDate>Mon, 29 Jan 2024 20:22:57 GMT</pubDate>
            <atom:updated>2024-02-06T20:40:14.960Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Turn a Custom GPT into a Note-Taking Super Assistant]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/use-ai/turn-a-custom-gpt-into-a-note-taking-super-assistant-634d9d184f13?source=rss----99315fefff4d---4"><img src="https://cdn-images-1.medium.com/max/1456/0*9HxPGIwiviCyj0Zc.png" width="1456"></a></p><p class="medium-feed-snippet">Supercharge and hyper-personalize your own custom GPT&#x2019;s. Leverage Make.com</p><p class="medium-feed-link"><a href="https://medium.com/use-ai/turn-a-custom-gpt-into-a-note-taking-super-assistant-634d9d184f13?source=rss----99315fefff4d---4">Continue reading on Use AI »</a></p></div>]]></description>
            <link>https://medium.com/use-ai/turn-a-custom-gpt-into-a-note-taking-super-assistant-634d9d184f13?source=rss----99315fefff4d---4</link>
            <guid isPermaLink="false">https://medium.com/p/634d9d184f13</guid>
            <category><![CDATA[obsidian]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[notes]]></category>
            <dc:creator><![CDATA[Sjoerd Tiemensma]]></dc:creator>
            <pubDate>Sun, 14 Jan 2024 17:56:34 GMT</pubDate>
            <atom:updated>2024-06-14T05:12:34.218Z</atom:updated>
        </item>
    </channel>
</rss>