Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
liamgriffiths committed Jun 18, 2024
1 parent 92bc89b commit 2b80ee0
Show file tree
Hide file tree
Showing 5 changed files with 593 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

async def amain():
response = await substrate.async_stream(a, b)
async for event in response.async_iter_events():
async for event in response.async_iter():
print(event)


Expand All @@ -31,7 +31,7 @@ async def amain():

def main():
response = substrate.stream(a, b)
for message in response.iter_events():
for message in response.iter():
print(message)


Expand Down
29 changes: 29 additions & 0 deletions examples/streaming/fastapi-example/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import sys
from pathlib import Path

from substrate.nodes import Llama3Instruct8B

# add parent dir to sys.path to make 'substrate' importable
parent_dir = Path(__file__).resolve().parent.parent.parent.parent
sys.path.insert(0, str(parent_dir))

api_key = os.environ.get("SUBSTRATE_API_KEY")
if api_key is None:
raise EnvironmentError("No SUBSTRATE_API_KEY set")

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

from substrate import Substrate, Llama3Instruct8B

app = FastAPI()
substrate = Substrate(api_key=api_key, timeout=60 * 5)

@app.get("/qotd")
def quote_of_the_day():
quote = Llama3Instruct8B(prompt="What's an inspirational quote of the day?")

response = substrate.stream(quote)

return StreamingResponse(response.iter_events(), media_type="text/event-stream")
Loading

0 comments on commit 2b80ee0

Please sign in to comment.