Skip to content

Commit 126df6c

Browse files
New Release 2023.07.30 (IBM#108)
Merging develop into main.
1 parent 524dff9 commit 126df6c

30 files changed

+326
-123
lines changed

EXTENSIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ IBM Generative AI extensions can be either of the following :
2323
Extensions that are meant for public use from the get-go should instead be developed as official open-source extensions. Examples of official extensions that have already been released are LangChain, Pandas, and Hugging Face extensions.
2424

2525
### Ownership and location
26-
Open-source extensions should be submitted directly to the [IBM Generative AI open-source repository](https://github.com/IBM/ibm-generative-ai) and should be developed following the [open-source Gen AI contribution guide](https://github.com/IBM/ibm-generative-ai/blob/main/DEVELOPMENT.md). Open-source official extensions are typically developed by the Gen AI team, or in collaboration with them. Providing maintenance to open-source official extensions is responsability of the Gen AI team.
26+
Open-source extensions should be submitted directly to the [IBM Generative AI open-source repository](https://github.com/IBM/ibm-generative-ai) and should be developed following the [open-source Gen AI contribution guide](https://github.com/IBM/ibm-generative-ai/blob/main/CONTRIBUTING.md). Open-source official extensions are typically developed by the Gen AI team, or in collaboration with them. Providing maintenance to open-source official extensions is responsability of the Gen AI team.
2727

2828
## Open-source "third-party" extensions
2929
All other extensions neither implemented nor officially maintained by the Gen AI team are referred to as open-source third-party extensions.

GETTING_STARTED.md

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
## <a name='TableofContents'></a>Table of Contents
44

5-
<!-- vscode-markdown-toc -->
65
* [Table of Contents](#table-of-contents)
76
* [Installation](#installation)
87
* [Gen AI Endpoint](#gen-ai-endpoint)
@@ -11,6 +10,7 @@
1110
* [Async Example](#async-example)
1211
* [Synchronous Example](#synchronous-example)
1312
* [Tips and Troubleshooting](#tips-and-troubleshooting)
13+
* [Model Availability](#model-availability)
1414
* [Enabling Logs](#enabling-logs)
1515
* [Experimenting with a Large Number of Prompts](#many-prompts)
1616
* [Extensions](#extensions)
@@ -22,10 +22,13 @@
2222
```bash
2323
pip install ibm-generative-ai
2424
```
25+
2526
#### <a name='KnownIssueFixes:'></a>Known Issue Fixes:
27+
2628
- **[SSL Issue]** If you run into "SSL_CERTIFICATE_VERIFY_FAILED" please run the following code snippet here: [support](SUPPORT.md).
2729

2830
### <a name='Prerequisites'></a>Prerequisites
31+
2932
Python version >= 3.9
3033

3134
Pip version >= 22.0.1
@@ -70,13 +73,11 @@ creds = Credentials(api_key=my_api_key, api_endpoint=my_api_endpoint)
7073

7174
```
7275

73-
7476
## <a name='Examples'></a>Examples
7577

7678
There are a number of examples you can try in the [`examples/user`](examples/user) directory.
7779
Login to [workbench.res.ibm.com](https://workbench.res.ibm.com/) and get your GenAI API key. Then, create a `.env` file and assign the `GENAI_KEY` value as below example. [More information](#gen-ai-endpoint)
7880

79-
8081
```ini
8182
GENAI_KEY=YOUR_GENAI_API_KEY
8283
# GENAI_API=GENAI_API_ENDPOINT << for a different endpoint
@@ -184,6 +185,40 @@ for response in responses:
184185

185186
## <a name='TipsAndTroubleshooting'></a>Tips and Troubleshooting
186187

188+
### <a name='Model Availability'></a>Model Availability
189+
To test the reachability of your endpoint and availability of desired model, use the following utility script with your model details:
190+
```python
191+
import os
192+
193+
from dotenv import load_dotenv
194+
195+
from genai.credentials import Credentials
196+
from genai.model import Model
197+
198+
# make sure you have a .env file under genai root with
199+
# GENAI_KEY=<your-genai-key>
200+
# GENAI_API=<your-genai-api endpoint>
201+
load_dotenv()
202+
api_key = os.getenv("GENAI_KEY", None)
203+
api_url = os.getenv("GENAI_API", None)
204+
creds = Credentials(api_key, api_endpoint=api_url)
205+
206+
print("======= List of all available models =======")
207+
for m in Model.models(credentials=creds):
208+
print(m)
209+
210+
print("====== Checking availability of a specific model =======")
211+
model_id = "<string-id-of-model>"
212+
model = Model(model_id, params=None, credentials=creds)
213+
print(f"Model availability for {model_id}: {model.available()}")
214+
215+
print("====== Display model card =======")
216+
model = Model(model_id, params=None, credentials=creds)
217+
model_info = model.info()
218+
print(f"Model info for {model_id}: \n{model_info}")
219+
print(f"Extract fields from model card (e.g., token_limit): {model_info.token_limit}")
220+
```
221+
187222
### <a name='EnablingLogs'></a>Enabling Logs
188223

189224
If you're building an application or example and would like to see the GENAI logs, you can enable them in the following way:
@@ -223,6 +258,7 @@ To learn more about logging in python, you can follow the tutorial [here](https:
223258

224259
Since generating responses for a large number of prompts can be time-consuming and there could be unforeseen circumstances such as internet connectivity issues, here are some strategies
225260
to work with:
261+
226262
- Start with a small number of prompts to prototype the code. You can enable logging as described above for debugging during prototyping.
227263
- Include exception handling in sensitive sections such as callbacks.
228264
- Checkpoint/save prompts and received responses periodically.
@@ -257,10 +293,13 @@ us if you want support for some framework as an extension or want to design an e
257293
### <a name='LangChainExtension'></a>LangChain Extension
258294

259295
Install the langchain extension as follows:
296+
260297
```bash
261298
pip install "ibm-generative-ai[langchain]"
262299
```
300+
263301
Currently the langchain extension allows IBM Generative AI models to be wrapped as Langchain LLMs and translation between genai PromptPatterns and LangChain PromptTemplates. Below are sample snippets
302+
264303
```python
265304
import os
266305
from dotenv import load_dotenv
@@ -292,13 +331,6 @@ print(langchain_model(template.format(question="What is life?")))
292331
print(genai_model.generate([pattern.sub("question", "What is life?")])[0].generated_text)
293332
```
294333

295-
## <a name='[Deprecated] Model Types'></a>[Deprecated] Model Types
296-
297-
Model types can be imported from the [ModelType class](src/genai/schemas/models.py). If you want to use a model that is not included in this class, you can pass it as a string as exemplified [here](src/genai/schemas/models.py).
298-
299-
Models can be selected by passing their string id to the Model class as exemplified [here](src/genai/schemas/models.py).
300-
301-
302334
## <a name='Support'></a>Support
303335

304336
Need help? Check out how to get [support](SUPPORT.md)

documentation/docs/source/rst_source/genai.schemas.models.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

documentation/docs/source/rst_source/genai.schemas.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Submodules
1010
genai.schemas.descriptions
1111
genai.schemas.generate_params
1212
genai.schemas.history_params
13-
genai.schemas.models
1413
genai.schemas.responses
1514
genai.schemas.token_params
1615
genai.schemas.tunes_params

examples/dev/async-flaky-request-handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dotenv import load_dotenv
88

99
from genai.model import Credentials, Model
10-
from genai.schemas import GenerateParams, ModelType, TokenParams
10+
from genai.schemas import GenerateParams, TokenParams
1111
from genai.services.connection_manager import ConnectionManager
1212
from genai.services.request_handler import RequestHandler
1313

@@ -80,7 +80,7 @@ async def flaky_async_generate(
8080
tokenize_params = TokenParams(return_tokens=True)
8181

8282

83-
flan_ul2 = Model(ModelType.FLAN_UL2, params=generate_params, credentials=creds)
83+
flan_ul2 = Model("google/flan-ul2", params=generate_params, credentials=creds)
8484
prompts = ["Generate a random number > {}: ".format(i) for i in range(25)]
8585
for response in flan_ul2.generate_async(prompts, ordered=True):
8686
pass

examples/dev/async-flaky-responses-ordered.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dotenv import load_dotenv
77

88
from genai.model import Credentials, GenAiException, Model
9-
from genai.schemas import GenerateParams, ModelType, TokenParams
9+
from genai.schemas import GenerateParams, TokenParams
1010
from genai.services.async_generator import AsyncResponseGenerator
1111

1212
num_requests = 0
@@ -83,7 +83,7 @@ def tokenize_async(self, prompts, ordered=False, callback=None, options=None):
8383
tokenize_params = TokenParams(return_tokens=True)
8484

8585

86-
flan_ul2 = FlakyModel(ModelType.FLAN_UL2_20B, params=generate_params, credentials=creds)
86+
flan_ul2 = FlakyModel("google/flan-ul2", params=generate_params, credentials=creds)
8787
prompts = ["Generate a random number > {}: ".format(i) for i in range(17)]
8888
print("======== Async Generate with ordered=True ======== ")
8989
counter = 0
@@ -97,7 +97,7 @@ def tokenize_async(self, prompts, ordered=False, callback=None, options=None):
9797
num_requests = 0
9898

9999
# Instantiate a model proxy object to send your requests
100-
flan_ul2 = FlakyModel(ModelType.FLAN_UL2_20B, params=tokenize_params, credentials=creds)
100+
flan_ul2 = FlakyModel("google/flan-ul2", params=tokenize_params, credentials=creds)
101101
prompts = ["Generate a random number > {}: ".format(i) for i in range(23)]
102102
print("======== Async Tokenize with ordered=True ======== ")
103103
counter = 0

examples/dev/generate-all-models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from dotenv import load_dotenv
44

55
from genai.model import Credentials, Model
6-
from genai.schemas import GenerateParams, ModelType
6+
from genai.schemas import GenerateParams
77

88
# make sure you have a .env file under genai root with
99
# GENAI_KEY=<your-genai-key>
@@ -24,7 +24,8 @@
2424
" during iteration it will do symb1 symb1 symb1 due to how it"
2525
" maps internally. ===="
2626
)
27-
for key, modelid in ModelType.__members__.items():
27+
for model_card in Model.models(credentials=creds):
28+
modelid = model_card.id
2829
model = Model(modelid, params=params, credentials=creds)
2930
responses = [response.generated_text for response in model.generate(prompts)]
3031
print(modelid, ":", responses)

examples/dev/logging_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dotenv import load_dotenv
55

66
from genai.model import Credentials, Model
7-
from genai.schemas import GenerateParams, ModelType
7+
from genai.schemas import GenerateParams
88

99
logging.basicConfig(level=logging.INFO)
1010

@@ -22,7 +22,7 @@
2222
params = GenerateParams(decoding_method="sample", max_new_tokens=10)
2323

2424
# Instantiate a model proxy object to send your requests
25-
flan_ul2 = Model(ModelType.FLAN_UL2, params=params, credentials=creds)
25+
flan_ul2 = Model("google/flan-ul2", params=params, credentials=creds)
2626

2727
prompts = ["Hello! How are you?", "How's the weather?"]
2828
for response in flan_ul2.generate_async(prompts):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
torch==2.0.1
2-
transformers==4.29.2
2+
transformers==4.30.0
33
sentencepiece==0.1.99

examples/user/model_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
from dotenv import load_dotenv
4+
5+
from genai.credentials import Credentials
6+
from genai.model import Model
7+
8+
# make sure you have a .env file under genai root with
9+
# GENAI_KEY=<your-genai-key>
10+
# GENAI_API=<your-genai-api endpoint>
11+
load_dotenv()
12+
api_key = os.getenv("GENAI_KEY", None)
13+
api_url = os.getenv("GENAI_API", None)
14+
creds = Credentials(api_key, api_endpoint=api_url)
15+
16+
print("======= List of all models =======")
17+
for m in Model.models(credentials=creds):
18+
print(m)
19+
20+
print("====== Checking model availability =======")
21+
model = Model("google/ul2", params=None, credentials=creds)
22+
print("Model availability for 'google/ul2': ", model.available())
23+
24+
model = Model("random", params=None, credentials=creds)
25+
print("Model availability for 'random': ", model.available())
26+
27+
print("====== Display model card =======")
28+
model = Model("google/ul2", params=None, credentials=creds)
29+
print("Model info for 'google/ul2': \n", model.info())

examples/user/prompt_templating/watsonx-prompt-output.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from dotenv import load_dotenv
44

5-
from genai.model import Credentials, Model
5+
from genai.credentials import Credentials
6+
from genai.model import Model
67
from genai.prompt_pattern import PromptPattern
7-
from genai.schemas import GenerateParams, ModelType
8+
from genai.schemas import GenerateParams
89

910
# make sure you have a .env file under genai root with
1011
# GENAI_KEY=<your-genai-key>
@@ -15,7 +16,7 @@
1516
creds = Credentials(api_key, api_endpoint=api_url)
1617
params = GenerateParams(temperature=0.5)
1718

18-
model = Model(ModelType.FLAN_UL2, params=params, credentials=creds)
19+
model = Model("google/flan-ul2", params=params, credentials=creds)
1920

2021

2122
_template = """

examples/user/prompt_templating/watsonx-prompt-pattern-ux-async.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
from dotenv import load_dotenv
55

6-
from genai.model import Credentials, Model
6+
from genai.credentials import Credentials
7+
from genai.model import Model
78
from genai.options import Options
89
from genai.prompt_pattern import PromptPattern
9-
from genai.schemas import GenerateParams, ModelType
10+
from genai.schemas import GenerateParams
1011

1112
# make sure you have a .env file under genai root with
1213
# GENAI_KEY=<your-genai-key>
@@ -18,7 +19,7 @@
1819
creds = Credentials(api_key, api_endpoint=api_url)
1920
params = GenerateParams(temperature=0.5)
2021

21-
model = Model(ModelType.FLAN_UL2, params=params, credentials=creds)
22+
model = Model("google/flan-ul2", params=params, credentials=creds)
2223

2324

2425
_template = """

examples/user/prompt_templating/watsonx-prompt-pattern-ux.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
from dotenv import load_dotenv
55

6-
from genai.model import Credentials, Model
6+
from genai.credentials import Credentials
7+
from genai.model import Model
78
from genai.options import Options
89
from genai.prompt_pattern import PromptPattern
9-
from genai.schemas import GenerateParams, ModelType
10+
from genai.schemas import GenerateParams
1011

1112
# make sure you have a .env file under genai root with
1213
# GENAI_KEY=<your-genai-key>
@@ -18,7 +19,7 @@
1819
creds = Credentials(api_key, api_endpoint=api_url)
1920
params = GenerateParams(temperature=0.5)
2021

21-
model = Model(ModelType.FLAN_UL2, params=params, credentials=creds)
22+
model = Model("google/flan-ul2", params=params, credentials=creds)
2223

2324

2425
_template = """

examples/user/prompt_tuning/classification.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,40 @@ def get_creds():
110110
if status in ["FAILED", "HALTED"]:
111111
print("Model tuning failed or halted")
112112
else:
113+
print("Model info:\n")
114+
print(tuned_model.info())
115+
time.sleep(5)
116+
113117
prompt = input("Enter a prompt:\n")
114118
genparams = GenerateParams(
115119
decoding_method="greedy",
116120
max_new_tokens=50,
117121
min_new_tokens=1,
118122
)
119123
print("Answer = ", tuned_model.generate([prompt])[0].generated_text)
124+
time.sleep(5)
125+
126+
print("~~~~~~~ List of all models ~~~~~~")
127+
for m in Model.models(credentials=creds):
128+
print(m, "\n")
129+
time.sleep(10)
120130

121-
print("~~~~~~~ Listing tunes and getting tune metadata with TuneManager ~~~~~")
131+
print("~~~~~~~ Getting list of all tuned models with TuneManager ~~~~~")
122132

123-
list_params = TunesListParams(limit=5, offset=0)
133+
list_params = TunesListParams(limit=50, offset=0)
124134

125135
tune_list = TuneManager.list_tunes(credentials=creds, params=list_params)
126136
print("\n\nList of tunes: \n\n")
127137
for tune in tune_list.results:
128138
print(tune, "\n")
139+
time.sleep(10)
129140

130141
tune_get_result = TuneManager.get_tune(credentials=creds, tune_id=tuned_model.model)
131142
print(
132143
"\n\n~~~~~ Metadata for a single tune with TuneManager ~~~~: \n\n",
133144
tune_get_result,
134145
)
146+
time.sleep(5)
135147

136148
print("~~~~~~~ Deleting a tuned model ~~~~~")
137149
to_delete = input("Delete this model? (y/N):\n")

innersource.yaml

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)