forked from IBM/ibm-generative-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_tokenize.py
30 lines (22 loc) · 915 Bytes
/
async_tokenize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
from dotenv import load_dotenv
from genai.model import Credentials, Model
from genai.schemas import ModelType, TokenParams
# make sure you have a .env file under genai root with
# GENAI_KEY=<your-genai-key>
load_dotenv()
api_key = os.getenv("GENAI_KEY", None)
print("\n------------- Example (Async Greetings)-------------\n")
params = TokenParams(return_tokens=True)
creds = Credentials(api_key)
model = Model(ModelType.FLAN_UL2, params=params, credentials=creds)
greeting = "Hello! How are you?"
lots_of_greetings = [greeting] * 100
num_of_greetings = len(lots_of_greetings)
num_said_greetings = 0
greeting1 = "Hello! How are you?"
# yields batch of results that are produced asynchronously and in parallel
for result in model.tokenize_async(lots_of_greetings):
num_said_greetings += 1
print(f"[Progress {str(float(num_said_greetings/num_of_greetings)*100)}%]")
print(f"\t {result}")