-
Notifications
You must be signed in to change notification settings - Fork 2
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
c38a66f
commit 92bc89b
Showing
7 changed files
with
151 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import sys | ||
import asyncio | ||
from pathlib import Path | ||
|
||
# add parent dir to sys.path to make 'substrate' importable | ||
parent_dir = Path(__file__).resolve().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 substrate import Substrate, GenerateText | ||
|
||
substrate = Substrate(api_key=api_key, timeout=60 * 5) | ||
|
||
|
||
a = GenerateText(prompt="tell me about windmills", max_tokens=10) | ||
b = GenerateText(prompt="tell me more about cereal", max_tokens=10) | ||
|
||
|
||
async def amain(): | ||
response = await substrate.async_stream(a, b) | ||
async for event in response.async_iter_events(): | ||
print(event) | ||
|
||
|
||
asyncio.run(amain()) | ||
|
||
|
||
def main(): | ||
response = substrate.stream(a, b) | ||
for message in response.iter_events(): | ||
print(message) | ||
|
||
|
||
main() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
import json | ||
from typing import Iterator, AsyncIterator | ||
|
||
import httpx_sse | ||
|
||
|
||
class ServerSentEvent: | ||
def __init__(self, event: httpx_sse.ServerSentEvent): | ||
self.event = event | ||
|
||
@property | ||
def data(self): | ||
return json.loads(self.event.data) | ||
|
||
def __repr__(self): | ||
return self.event.__repr__() | ||
|
||
def __str__(self): | ||
""" | ||
Render the Server-Sent Event as a string to be rendered in a streaming response | ||
""" | ||
fields = ["id", "event", "data", "retry"] | ||
lines = [f"{field}: {getattr(self.event, field)}" for field in fields if getattr(self.event, field)] | ||
return "\n".join(lines) + "\n" | ||
|
||
|
||
class SubstrateStreamingResponse: | ||
""" | ||
Substrate stream response. | ||
""" | ||
|
||
def __init__(self, *, iterator): | ||
self.iterator = iterator | ||
|
||
def iter_events(self) -> Iterator[ServerSentEvent]: | ||
for sse in self.iterator: | ||
yield ServerSentEvent(sse) | ||
|
||
async def async_iter_events(self) -> AsyncIterator[ServerSentEvent]: | ||
async for sse in self.iterator: | ||
yield ServerSentEvent(sse) |
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