-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add simple text generation example (#323)
Ref: #321
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
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,42 @@ | ||
""" | ||
Simple text generation | ||
""" | ||
|
||
from dotenv import load_dotenv | ||
|
||
from genai.client import Client | ||
from genai.credentials import Credentials | ||
from genai.schema import ( | ||
TextGenerationParameters, | ||
TextGenerationReturnOptions, | ||
) | ||
|
||
|
||
def heading(text: str) -> str: | ||
"""Helper function for centering text.""" | ||
return "\n" + f" {text} ".center(80, "=") + "\n" | ||
|
||
|
||
# make sure you have a .env file under genai root with | ||
# GENAI_KEY=<your-genai-key> | ||
# GENAI_API=<genai-api-endpoint> | ||
load_dotenv() | ||
client = Client(credentials=Credentials.from_env()) | ||
|
||
print(heading("Simple Text Generation")) | ||
# yields batch of results that are produced asynchronously and in parallel | ||
for response in client.text.generation.create( | ||
model_id="google/flan-t5-xl", | ||
inputs=["What is a molecule?", "What is NLP?"], | ||
parameters=TextGenerationParameters( | ||
max_new_tokens=150, | ||
min_new_tokens=20, | ||
return_options=TextGenerationReturnOptions( | ||
input_text=True, | ||
), | ||
), | ||
): | ||
result = response.results[0] | ||
print(f"Input Text: {result.input_text}") | ||
print(f"Generated Text: {result.generated_text}") | ||
print("") |