-
Notifications
You must be signed in to change notification settings - Fork 306
implements async python sdk using asyncio #660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prathikr
wants to merge
6
commits into
main
Choose a base branch
from
prathikrao/python-sdk-asyncio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,53 +1,59 @@ | ||
| # <complete_code> | ||
| # <imports> | ||
| import asyncio | ||
| import sys | ||
| from foundry_local_sdk import Configuration, FoundryLocalManager | ||
| # </imports> | ||
|
|
||
|
|
||
| # <init> | ||
| # Initialize the Foundry Local SDK | ||
| config = Configuration(app_name="foundry_local_samples") | ||
| FoundryLocalManager.initialize(config) | ||
| manager = FoundryLocalManager.instance | ||
|
|
||
| # Download and register all execution providers. | ||
| current_ep = "" | ||
| def _ep_progress(ep_name: str, percent: float): | ||
| global current_ep | ||
| if ep_name != current_ep: | ||
| if current_ep: | ||
| print() | ||
| current_ep = ep_name | ||
| print(f"\r {ep_name:<30} {percent:5.1f}%", end="", flush=True) | ||
|
|
||
| manager.download_and_register_eps(progress_callback=_ep_progress) | ||
| if current_ep: | ||
| print() | ||
| async def main(): | ||
| # <init> | ||
| # Initialize the Foundry Local SDK | ||
| config = Configuration(app_name="foundry_local_samples") | ||
| await FoundryLocalManager.initialize(config) | ||
| manager = FoundryLocalManager.instance | ||
|
|
||
| # Download and register all execution providers. | ||
| current_ep = "" | ||
| def _ep_progress(ep_name: str, percent: float): | ||
| nonlocal current_ep | ||
| if ep_name != current_ep: | ||
| if current_ep: | ||
| print() | ||
| current_ep = ep_name | ||
| print(f"\r {ep_name:<30} {percent:5.1f}%", end="", flush=True) | ||
|
|
||
| # Load the whisper model for speech-to-text | ||
| model = manager.catalog.get_model("whisper-tiny") | ||
| model.download( | ||
| lambda progress: print( | ||
| f"\rDownloading model: {progress:.2f}%", | ||
| end="", | ||
| flush=True, | ||
| await manager.download_and_register_eps(progress_callback=_ep_progress) | ||
| if current_ep: | ||
| print() | ||
|
|
||
| # Load the whisper model for speech-to-text | ||
| model = await manager.catalog.get_model("whisper-tiny") | ||
| await model.download( | ||
| lambda progress: print( | ||
| f"\rDownloading model: {progress:.2f}%", | ||
| end="", | ||
| flush=True, | ||
| ) | ||
| ) | ||
| ) | ||
| print() | ||
| model.load() | ||
| print("Model loaded.") | ||
| # </init> | ||
|
|
||
| # <transcription> | ||
| # Get the audio client and transcribe | ||
| audio_client = model.get_audio_client() | ||
| audio_file = sys.argv[1] if len(sys.argv) > 1 else "Recording.mp3" | ||
| result = audio_client.transcribe(audio_file) | ||
| print("Transcription:") | ||
| print(result.text) | ||
| # </transcription> | ||
|
|
||
| # Clean up | ||
| model.unload() | ||
| print() | ||
| await model.load() | ||
| print("Model loaded.") | ||
| # </init> | ||
|
|
||
| # <transcription> | ||
| # Get the audio client and transcribe | ||
| audio_client = model.get_audio_client() | ||
| audio_file = sys.argv[1] if len(sys.argv) > 1 else "Recording.mp3" | ||
| result = await audio_client.transcribe(audio_file) | ||
| print("Transcription:") | ||
| print(result.text) | ||
| # </transcription> | ||
|
|
||
| # Clean up | ||
| await model.unload() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
| # </complete_code> |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,73 +1,80 @@ | ||
| # <complete_code> | ||
| # <imports> | ||
| import asyncio | ||
| from foundry_local_sdk import Configuration, FoundryLocalManager | ||
| from langchain_openai import ChatOpenAI | ||
| from langchain_core.prompts import ChatPromptTemplate | ||
| from langchain_core.output_parsers import StrOutputParser | ||
| # </imports> | ||
|
|
||
| # <init> | ||
| # Initialize the Foundry Local SDK | ||
| config = Configuration(app_name="foundry_local_samples") | ||
| FoundryLocalManager.initialize(config) | ||
| manager = FoundryLocalManager.instance | ||
|
|
||
| # Download and register all execution providers. | ||
| current_ep = "" | ||
| def _ep_progress(ep_name: str, percent: float): | ||
| global current_ep | ||
| if ep_name != current_ep: | ||
| if current_ep: | ||
| print() | ||
| current_ep = ep_name | ||
| print(f"\r {ep_name:<30} {percent:5.1f}%", end="", flush=True) | ||
| async def main(): | ||
| # <init> | ||
| # Initialize the Foundry Local SDK | ||
| config = Configuration(app_name="foundry_local_samples") | ||
| await FoundryLocalManager.initialize(config) | ||
| manager = FoundryLocalManager.instance | ||
|
|
||
| manager.download_and_register_eps(progress_callback=_ep_progress) | ||
| if current_ep: | ||
| # Download and register all execution providers. | ||
| current_ep = "" | ||
| def _ep_progress(ep_name: str, percent: float): | ||
| nonlocal current_ep | ||
| if ep_name != current_ep: | ||
| if current_ep: | ||
| print() | ||
| current_ep = ep_name | ||
| print(f"\r {ep_name:<30} {percent:5.1f}%", end="", flush=True) | ||
|
|
||
| await manager.download_and_register_eps(progress_callback=_ep_progress) | ||
| if current_ep: | ||
| print() | ||
|
|
||
| # Load a model | ||
| model = await manager.catalog.get_model("qwen2.5-0.5b") | ||
| await model.download( | ||
| lambda progress: print( | ||
| f"\rDownloading model: {progress:.2f}%", | ||
| end="", | ||
| flush=True, | ||
| ) | ||
| ) | ||
| print() | ||
| await model.load() | ||
| print("Model loaded.") | ||
|
|
||
| # Start the web service to expose an OpenAI-compatible endpoint | ||
| await manager.start_web_service() | ||
| base_url = f"{manager.urls[0]}/v1" | ||
| # </init> | ||
|
|
||
| # Load a model | ||
| model = manager.catalog.get_model("qwen2.5-0.5b") | ||
| model.download( | ||
| lambda progress: print( | ||
| f"\rDownloading model: {progress:.2f}%", | ||
| end="", | ||
| flush=True, | ||
| # <langchain_setup> | ||
| # Create a LangChain ChatOpenAI instance pointing to the local endpoint | ||
| llm = ChatOpenAI( | ||
| base_url=base_url, | ||
| api_key="none", | ||
| model=model.id, | ||
| ) | ||
| ) | ||
| print() | ||
| model.load() | ||
| print("Model loaded.") | ||
| # </langchain_setup> | ||
|
|
||
| # Start the web service to expose an OpenAI-compatible endpoint | ||
| manager.start_web_service() | ||
| base_url = f"{manager.urls[0]}/v1" | ||
| # </init> | ||
| # <chat_completion> | ||
| # Create a translation chain | ||
| prompt = ChatPromptTemplate.from_messages([ | ||
| ("system", "You are a translator. Translate the following text to {language}. Only output the translation, nothing else."), | ||
| ("user", "{text}") | ||
| ]) | ||
|
|
||
| # <langchain_setup> | ||
| # Create a LangChain ChatOpenAI instance pointing to the local endpoint | ||
| llm = ChatOpenAI( | ||
| base_url=base_url, | ||
| api_key="none", | ||
| model=model.id, | ||
| ) | ||
| # </langchain_setup> | ||
| chain = prompt | llm | StrOutputParser() | ||
|
|
||
| # <chat_completion> | ||
| # Create a translation chain | ||
| prompt = ChatPromptTemplate.from_messages([ | ||
| ("system", "You are a translator. Translate the following text to {language}. Only output the translation, nothing else."), | ||
| ("user", "{text}") | ||
| ]) | ||
| # Run the chain | ||
| result = chain.invoke({"language": "Spanish", "text": "Hello, how are you today?"}) | ||
|
prathikr marked this conversation as resolved.
Outdated
|
||
| print(f"Translation: {result}") | ||
| # </chat_completion> | ||
|
|
||
| chain = prompt | llm | StrOutputParser() | ||
| # Clean up | ||
| await model.unload() | ||
| await manager.stop_web_service() | ||
|
|
||
| # Run the chain | ||
| result = chain.invoke({"language": "Spanish", "text": "Hello, how are you today?"}) | ||
| print(f"Translation: {result}") | ||
| # </chat_completion> | ||
|
|
||
| # Clean up | ||
| model.unload() | ||
| manager.stop_web_service() | ||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
| # </complete_code> | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.