Skip to content

Commit

Permalink
add fastapi, langchain, fix md lint
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettgu10 committed Mar 29, 2024
1 parent 061f3c5 commit d1902d1
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 3 deletions.
4 changes: 3 additions & 1 deletion 01-hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ example is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.

## How to Run

First ensure that your Wrangler version is up to date (3.30.0 and above).
```

```bash
$ wrangler -v
⛅️ wrangler 3.30.0
```
Expand Down
6 changes: 4 additions & 2 deletions 02-binding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ example is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.

## How to Run

First ensure that your Wrangler version is up to date (3.30.0 and above).
```

```bash
$ wrangler -v
⛅️ wrangler 3.30.0
```
Expand All @@ -16,4 +18,4 @@ Now, you must set up a [KV namespace](https://developers.cloudflare.com/kv/refer
Now, if you run `wrangler dev` within this directory, it should use the config
in `wrangler.toml` to run the example.

You can also run `wrangler deploy` to deploy the example.
You can also run `wrangler deploy` to deploy the example.
19 changes: 19 additions & 0 deletions 03-fastapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# FastAPI Example

Warning: Python support in Workers is experimental and things will break. This
demo is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.

## How to Run

First ensure that your Wrangler version is up to date (3.30.0 and above).

```bash
$ wrangler -v
⛅️ wrangler 3.30.0
```

Now, if you run `wrangler dev` within this directory, it should use the config
in `wrangler.toml` to run the demo.

You can also run `wrangler deploy` to deploy the demo.
1 change: 1 addition & 0 deletions 03-fastapi/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastapi
39 changes: 39 additions & 0 deletions 03-fastapi/src/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from fastapi import FastAPI, Request
from pydantic import BaseModel

async def on_fetch(request, env):
import asgi

return await asgi.fetch(app, request, env)

app = FastAPI()

@app.get("/")
async def root():
return {"message": "Hello, World!"}

@app.get("/env")
async def root(req: Request):
env = req.scope["env"]
return {"message": "Here is an example of getting an environment variable: " + env.MESSAGE}

class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None

@app.post("/items/")
async def create_item(item: Item):
return item

@app.put("/items/{item_id}")
async def create_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result

@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
7 changes: 7 additions & 0 deletions 03-fastapi/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "hello-fastapi"
main = "src/worker.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2023-12-18"

[vars]
MESSAGE = "This is an environment variable"
1 change: 1 addition & 0 deletions 04-langchain/.dev.vars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_KEY=abc123
27 changes: 27 additions & 0 deletions 04-langchain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Langchain Example

Warning: Python support in Workers is experimental and things will break. This
demo is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.

## How to Run

First ensure that your Wrangler version is up to date (3.30.0 and above).

```bash
$ wrangler -v
⛅️ wrangler 3.30.0
```

Now, if you run `wrangler dev` within this directory, it should use the config
in `wrangler.toml` to run the demo.

This demo uses a [Workers secret](https://developers.cloudflare.com/workers/configuration/secrets/)
to configure the API key. Before deployment you must set this key using the
Wrangler CLI:

```bash
$ wrangler secret put API_KEY
```

You can now run `wrangler deploy` to deploy the demo.
2 changes: 2 additions & 0 deletions 04-langchain/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
langchain_core
langchain_openai
11 changes: 11 additions & 0 deletions 04-langchain/src/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from js import Response
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

async def on_fetch(request, env):
prompt = PromptTemplate.from_template("Complete the following sentence: I am a {profession} and ")
llm = OpenAI(api_key=env.API_KEY)
chain = prompt | llm

res = await chain.ainvoke({"profession": "electrician"})
return Response.new(res.split(".")[0].strip())
4 changes: 4 additions & 0 deletions 04-langchain/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "langchain-python-worker"
main = "src/worker.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2024-01-29"

0 comments on commit d1902d1

Please sign in to comment.