-
Notifications
You must be signed in to change notification settings - Fork 130
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
3638ab6
commit 660f063
Showing
4 changed files
with
90 additions
and
4 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
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,34 @@ | ||
import asyncio | ||
import os | ||
|
||
from langchain.prompts import ChatPromptTemplate | ||
from langchain.schema import StrOutputParser | ||
from langchain_cohere import ChatCohere | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
from traceloop.sdk import Traceloop | ||
|
||
|
||
def streaming(runnable, input_prompt): | ||
return ''.join(list(runnable.stream(input_prompt))) | ||
|
||
|
||
async def astreaming(runnable, input_prompt): | ||
return ''.join([i async for i in runnable.astream(input_prompt)]) | ||
|
||
|
||
if __name__ == "__main__": | ||
Traceloop.init( | ||
app_name="streaming_example", | ||
exporter=None if os.getenv("TRACELOOP_API_KEY") else ConsoleSpanExporter(), | ||
) | ||
|
||
prompt = ChatPromptTemplate.from_messages( | ||
[("system", "You are a helpful assistant"), ("user", "{input}")] | ||
) | ||
chat = ChatCohere(model="command-r") | ||
parser = StrOutputParser() | ||
runnable = prompt | chat | parser | ||
input_prompt = {"input": "tell me a short joke"} | ||
|
||
print(streaming(runnable, input_prompt)) | ||
print(asyncio.run(astreaming(runnable, input_prompt))) |