Skip to content

Commit

Permalink
docs: add simple text generation example (#323)
Browse files Browse the repository at this point in the history
Ref: #321
  • Loading branch information
Tomas2D committed Feb 19, 2024
1 parent a38c3c3 commit 3e2b677
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/text/generation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Generate text using a model
Complex text generation example with a progress bar
Runs text generation asynchronously in parallel to achieve better performance
"""
Expand Down
42 changes: 42 additions & 0 deletions examples/text/simple_generation.py
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("")

0 comments on commit 3e2b677

Please sign in to comment.