@@ -6,49 +6,53 @@ FastAPI (Searchbot)
66
77:edb-alt-title: Building a searchbot with memory using FastAPI and Gel AI
88
9- In this tutorial we're going to walk you through building a chat bot app from
10- scratch using Gel and `FastAPI <https://fastapi.tiangolo.com/ >`_.
9+ In this tutorial we're going to walk you through building a chat bot with search
10+ capabilities using Gel and `FastAPI <https://fastapi.tiangolo.com/ >`_.
1111
12- FastAPI is a web framework designed to help you build an API , well, fast. And
13- Gel is a data layer designed to help you manage data in your application, and
14- also do it fast. By the end of this tutorial you will have tried out different
15- aspects of using those two together, and hopefully come out with something
16- useful on the other end.
12+ FastAPI is a framework designed to help you build a web app , well, fast. And Gel
13+ is a data layer designed to help you figure out storage in your application,
14+ and also do it fast. By the end of this tutorial you will have tried out
15+ different aspects of using those two together, and hopefully come out with
16+ something useful on the other end.
1717
1818We're going to start by creating an app with FastAPI, then add web search
19- capabilities to it, and then put search results through an LLM to get a
20- human-friendly answer. After that we'll tie it all together with Gel by adding
21- chat history, and polish it off with semantic search, so that the bot can
22- remember previous interactions with the user.
19+ capabilities to it, and then put search results through a language model to get
20+ a human-friendly answer. After that we'll use Gel to add chat history, and
21+ finish it off with semantic search, so that the bot can remember previous
22+ interactions with the user.
2323
24- The end result is going to look something like this.
24+ The end result is going to look something like this:
25+
26+ .. image ::
27+ /docs/tutorials/placeholder.png
28+ :alt: Placeholder
29+ :width: 100%
2530
2631Step 1. Initialize the project
2732==============================
2833
29- We're going to start by installing uv - a Python package manager that's going to
30- simplify environment management for us. Follow their
31- `installation instructions < https://docs.astral.sh/uv/getting-started/installation/ >`_
32- or simply run:
34+ We're going to start by installing ` uv < https://docs.astral.sh/uv/ >`_ - a Python
35+ package manager that's going to simplify environment management for us. You can
36+ follow their `installation instructions
37+ <https://docs.astral.sh/uv/getting-started/installation/> `_ or simply run:
3338
3439.. code-block :: bash
3540 $ curl -LsSf https://astral.sh/uv/install.sh | sh
3641
37- Now let's initialize our project following the
38- `documentation <https://docs.astral.sh/uv/guides/projects/ >`_:
42+ Once that is done, we can use uv to create scaffolding for our project following
43+ the `documentation <https://docs.astral.sh/uv/guides/projects/ >`_:
3944
4045.. code-block :: bash
4146 $ uv init searchbot \
4247 && cd searchbot
4348
44- For now, the only dependencies we know we're going to need are Gel and FastAPI,
45- so let's add those following uv's instructions on `managing dependencies
49+ For now, we know we're going to need are Gel and FastAPI, so let's add those
50+ following uv's instructions on `managing dependencies
4651<https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies> `_,
4752as well as FastAPI's `installation docs
48- <https://fastapi.tiangolo.com/#installation> `_. We'll follow that by ``uv sync ``
49- that's going to create our virtual environment in a ``.venv `` directory and
50- ensure it's ready. Finally, we'll activate the environment and get started with
51- code.
53+ <https://fastapi.tiangolo.com/#installation> `_. Running ``uv sync `` after that
54+ will create our virtual environment in a ``.venv `` directory and ensure it's
55+ ready. Finally, we'll activate the environment and get started.
5256
5357.. code-block :: bash
5458 $ uv add fastapi --optional standard \
@@ -59,17 +63,15 @@ code.
5963 Step 2. Get started with FastAPI
6064================================
6165
62- At this stage we're going to follow FastAPI's documentation. It contains
63- everything there is to know about building an application, but we'll quickly
64- touch on things that are relevant to us anyway.
66+ At this stage we need to follow FastAPI's `tutorial
67+ <https://fastapi.tiangolo.com/tutorial/> `_.
6568
66- We're going to make a super simple application with one endpoint that takes in a
67- user query as an input and returns it as an output. First, let's create a file
68- called `main.py ` and put the "Hello World" example in it:
69+ We're going to make a super simple app with one endpoint that takes in a user
70+ query as input and echoes it as an output. First, let's create a file called
71+ `main.py ` inside out ` app ` directory and put the "Hello World" example in it:
6972
7073.. code-block :: python
71- :caption: dbschema/ default.esdl
72- # where does this file live?
74+ :caption: app/ main.py
7375
7476 from fastapi import FastAPI
7577
@@ -85,31 +87,31 @@ To start the server, we need to run:
8587.. code-block :: bash
8688 $ fastapi dev main.py
8789
88- And sure enough, once the server is running, we can send a `GET ` request to our
89- server:
90-
91- .. note ::
92- Replace with built-in tooling
90+ Once the server gets up and running, we can make sure it works using FastAPI's
91+ built-in UI at <http://127.0.0.1:8000/docs>_, or simply using `curl `:
9392
9493.. code-block :: bash
9594 $ curl -X GET " http://localhost:8000/"
9695
97- ... and receive the output:
96+ { " message " : " Hello World " }
9897
99- .. code-block :: bash
100- # output
10198
102- In order to create an actual endpoint we need to tell the app that we're
103- expecting a query to come in as a parameter. We'd prefer to have it in the body
104- of the request, too, since user messages can get pretty long.
99+ Now, in order to create the endpoint we set out to create, we need to pass our
100+ query as a parameter to it . We'd prefer to have it in the body of the request
101+ since user messages can get pretty long.
105102
106- In FastAPI land this is done by creating a Pydantic schema and setting it as an
107- input parameter type. `Pydantic <https://docs.pydantic.dev/latest/ >`_ is a data
108- validation library for Python that's similar to standard dataclasses. It has
109- many features, but we're going to use it in a really straightforward manner to
110- set the input and the output schema of our endpoint:
103+ In FastAPI land this is done by creating a Pydantic schema and making it the
104+ type of the input parameter. `Pydantic <https://docs.pydantic.dev/latest/ >`_ is
105+ a data validation library for Python that's similar to standard dataclasses. It
106+ has many features, but we don't actually need to know about them for now. All we
107+ need to know is that FastAPI uses Pydantic types to automatically figure out
108+ schemae for `input <https://fastapi.tiangolo.com/tutorial/body/ >`_, as well as
109+ `output <https://fastapi.tiangolo.com/tutorial/response-model/ >`_.
110+
111+ Let's add the following to our `main.py `:
111112
112113.. code-block :: python
114+ :caption: app/ main.py
113115 from pydantic import BaseModel
114116
115117
@@ -120,23 +122,23 @@ set the input and the output schema of our endpoint:
120122 response: str | None = None
121123 sources: list[str ] | None = None
122124
123- Still following the docs, we'll beef up the endpoint like this:
125+ Now we can define our endpoint and set the two classes we just added as its
126+ argument and return type.
124127
125128.. code-block :: python
126129 @app.post (" /search" )
127130 async def search (search_terms : SearchTerms) -> SearchResult:
128131 return SearchResult(response = search_terms.query)
129132
130- And now let's test it:
133+ Same as before, we can test the endpoint using the UI, or by sending a request
134+ with `curl `:
131135
132136.. code-block :: bash
133137 $ curl -X POST " http://localhost:8000/search" \
134138 -H " Content-Type: application/json" \
135139 -d ' {"query": "test search"}'
136- ```
137140
138- .. code-block :: bash
139- {" response" :" test search" ," sources" :null," llm_error" :null}
141+ {" response" :" test search" ," sources" :null}
140142
141143 Step 3. Implement web search
142144============================
0 commit comments