-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
061f3c5
commit d1902d1
Showing
11 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fastapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
API_KEY=abc123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
langchain_core | ||
langchain_openai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |