Skip to content
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

Update tunes api #102

Merged
merged 80 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
dd8b702
Merge pull request #1 from IBM/develop
onkarbhardwaj Jun 22, 2023
1ae83f9
Merge branch 'IBM:develop' into develop
onkarbhardwaj Jul 3, 2023
b353c3e
init prompt tuning: add examples -files and tunes
mirianfsilva Jul 3, 2023
eade6f7
add schemas
mirianfsilva Jul 3, 2023
cce1c01
init file services
mirianfsilva Jul 3, 2023
9d85108
init tune services
mirianfsilva Jul 3, 2023
edda015
feat: tune manager
moneill0 Jul 3, 2023
7878761
fix: fixed tunes router
moneill0 Jul 3, 2023
e36af10
fix: fix merge conflicts
moneill0 Jul 3, 2023
e79844d
update imports from file example, update routers
mirianfsilva Jul 3, 2023
afd0571
add tests for files
mirianfsilva Jul 4, 2023
1dcec66
update tunes doc-strings
mirianfsilva Jul 4, 2023
72eb7ce
request handler changes - add files to httpx POST methods
mirianfsilva Jul 4, 2023
c80824f
update files docstrings, improve tests
mirianfsilva Jul 4, 2023
1cc0390
fix delete file tests
mirianfsilva Jul 4, 2023
46a46f2
add tunes tests
mirianfsilva Jul 4, 2023
ddae508
tunes examples updates
mirianfsilva Jul 4, 2023
9e0ddd4
add new file-tune example
mirianfsilva Jul 4, 2023
860a869
add descriptions and adjust imports
onkarbhardwaj Jul 5, 2023
1056493
feat: get tune methods function
moneill0 Jul 6, 2023
ba58545
fix: fixed get methods endpoint url
moneill0 Jul 6, 2023
b0f4b76
fix pre-commit
mirianfsilva Jul 6, 2023
ca5dc1a
tune() and status() methods for model tuning
Jul 6, 2023
1e697ee
documentation for status() method
Jul 6, 2023
9e7869d
feat: download tune assets function
moneill0 Jul 6, 2023
74fb5d7
unit tests
Jul 6, 2023
294c1d4
test: tune manager pytests
moneill0 Jul 6, 2023
26b5373
improve response helper for tests
mirianfsilva Jul 7, 2023
2500b87
fix: fixed get tunes schema check
moneill0 Jul 7, 2023
268eb76
Merge pull request #2 from onkarbhardwaj/tune_manager_pytest
mirianfsilva Jul 7, 2023
e40ea67
changing type in schemas for some fields
Jul 7, 2023
3cc9864
Merge pull request #3 from onkarbhardwaj/feature/prompt-tuning-model
onkarbhardwaj Jul 7, 2023
57d31f4
Prompt tuning for classification
Jul 7, 2023
1a00e22
delete tune and change paramter name
Jul 7, 2023
f1b0b49
Merge pull request #4 from onkarbhardwaj/feature/prompt-tuning-model
onkarbhardwaj Jul 7, 2023
f057609
accept ServiceInterface as credentials - FileManager
mirianfsilva Jul 7, 2023
fdad69e
accept ServiceInterface as credentials - TuneManager
mirianfsilva Jul 10, 2023
5f8661e
Merge pull request #5 from onkarbhardwaj/init-service
mirianfsilva Jul 10, 2023
99a1e15
update examples
mirianfsilva Jul 10, 2023
e879b06
FileManager and TuneManager can take both creds and servie interface
Jul 10, 2023
ec7f9c6
align examples with changed method signatures
Jul 10, 2023
6faf133
update schemas, docs strings, error messages
mirianfsilva Jul 10, 2023
20701a5
Examples for classification and summarization
Jul 10, 2023
91670ae
Revert "accept ServiceInterface as credentials - FileManager, TuneMan…
onkarbhardwaj Jul 10, 2023
0597c25
Merge pull request #7 from onkarbhardwaj/revert-5-init-service
onkarbhardwaj Jul 10, 2023
fad838c
Merge pull request #8 from onkarbhardwaj/feature/prompt-tuning-model
onkarbhardwaj Jul 10, 2023
a9c2bb6
remove examples that were superseded
Jul 10, 2023
4095d5c
Merge pull request #9 from onkarbhardwaj/feature/prompt-tuning-model
onkarbhardwaj Jul 10, 2023
8ad6014
update branch
mirianfsilva Jul 10, 2023
8444216
update docstrings
mirianfsilva Jul 10, 2023
4c86a1a
wrote examples for download assets
moneill0 Jul 10, 2023
4087147
update schema
mirianfsilva Jul 10, 2023
ca84efd
Merge pull request #6 from onkarbhardwaj/review
mirianfsilva Jul 10, 2023
8d9250f
update tests
mirianfsilva Jul 10, 2023
d56564c
Merge branch 'IBM:develop' into develop
onkarbhardwaj Jul 10, 2023
08678f7
Merge pull request #11 from onkarbhardwaj/develop
onkarbhardwaj Jul 10, 2023
435ec16
Moved sanitize param to utils folder to remove circular import
Jul 10, 2023
f81b360
Merge pull request #12 from onkarbhardwaj/feature/prompt-tuning-vdemers
onkarbhardwaj Jul 10, 2023
4ae4c39
docstrings and function signatures
Jul 10, 2023
43421db
Merge pull request #13 from onkarbhardwaj/fixes/docstrings-signatures
onkarbhardwaj Jul 10, 2023
53d79ac
Merge branch 'feature/prompt-tuning' into update_tunes_api
moneill0 Jul 11, 2023
1be3fc3
test: pytest for get tune methods
moneill0 Jul 11, 2023
076dd05
Merge branch 'IBM:develop' into develop
onkarbhardwaj Jul 11, 2023
f8251b9
fix: fixed download tune assets, add path argument
moneill0 Jul 12, 2023
5c541d1
style: cleaned code and added helper functions
moneill0 Jul 13, 2023
8625fb7
Merge branch 'main' into update_tunes_api
moneill0 Jul 13, 2023
1451c48
update branch with develop
mirianfsilva Jul 13, 2023
ea1190e
fix: removed files added to commit by mistake
moneill0 Jul 14, 2023
58710e7
refactor: handling of default argument and pytest
moneill0 Jul 17, 2023
d40cb0c
fix: fixed download assets pytest
moneill0 Jul 17, 2023
8562c35
fix: fixed request handler
moneill0 Jul 18, 2023
d754bf3
Merge branch 'develop' into update_tunes_api
onkarbhardwaj Jul 27, 2023
0aaa357
Update responses.py
onkarbhardwaj Aug 2, 2023
7c45048
Updates: examples, doc strings and tune manager
mirianfsilva Aug 3, 2023
921b771
Updates: examples, doc strings and tune manager
mirianfsilva Aug 3, 2023
2d5da28
update tests cases, improve tune downloads and tune delete
mirianfsilva Aug 3, 2023
ac04a9a
merge develop
mirianfsilva Aug 3, 2023
243a028
add download option in model class
mirianfsilva Aug 4, 2023
75ed79c
Update schemas
mirianfsilva Aug 7, 2023
0809717
run pre-commit
mirianfsilva Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions ACTIVE_PROJECT_TEAM.md

This file was deleted.

2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the [project team](./ACTIVE_PROJECT_TEAM.md). All
reported by contacting the [project team](./STARTER_TEAM.md). All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Expand Down
1 change: 0 additions & 1 deletion DEVELOPMENT.md → CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# IBM Generative AI Development Guide

<!-- vscode-markdown-toc -->
Expand Down
304 changes: 304 additions & 0 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
# Getting Started Guideline

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

<!-- vscode-markdown-toc -->
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Gen AI Endpoint](#gen-ai-endpoint)
* [Example](#example)
* [Examples](#examples)
* [Async Example](#async-example)
* [Synchronous Example](#synchronous-example)
* [Tips and Troubleshooting](#tips-and-troubleshooting)
* [Enabling Logs](#enabling-logs)
* [Experimenting with a Large Number of Prompts](#many-prompts)
* [Extensions](#extensions)
* [LangChain Extension](#langchain-extension)
* [Support](#support)

## <a name='Installation'></a>Installation

```bash
pip install ibm-generative-ai
```
#### <a name='KnownIssueFixes:'></a>Known Issue Fixes:
- **[SSL Issue]** If you run into "SSL_CERTIFICATE_VERIFY_FAILED" please run the following code snippet here: [support](SUPPORT.md).

### <a name='Prerequisites'></a>Prerequisites
Python version >= 3.9

Pip version >= 22.0.1

Check your pip version with `pip --version` and if needed run the following command to upgrade pip.

```bash
pip install --upgrade "pip>=22.0.1"
```

## <a name='GenAIEndpoint'></a>Gen AI Endpoint

By default, IBM Generative AI will use the following API endpoint: `https://workbench-api.res.ibm.com/v1/`. However, if you wish to target a different Gen AI API, you can do so by defining it with the `api_endpoint` argument when you instansiate the `Credentials` object.

### <a name='Example'></a>Example

Your `.env` file:

```ini
GENAI_KEY=YOUR_GENAI_API_KEY
GENAI_API=https://workbench-api.res.ibm.com/v1/
```

```python
import os

from dotenv import load_dotenv

from genai.model import Credentials

# make sure you have a .env file under genai root with
# GENAI_KEY=<your-genai-key>
# GENAI_API=<genai-api-endpoint>
load_dotenv()
my_api_key = os.getenv("GENAI_KEY", None)
my_api_endpoint = os.getenv("GENAI_API", None)

# creds object
creds = Credentials(api_key=my_api_key, api_endpoint=my_api_endpoint)

# Now start using GenAI!

```


## <a name='Examples'></a>Examples

There are a number of examples you can try in the [`examples/user`](examples/user) directory.
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)


```ini
GENAI_KEY=YOUR_GENAI_API_KEY
# GENAI_API=GENAI_API_ENDPOINT << for a different endpoint
```

### <a name='AsyncExample'></a>Async Example

```python
import os

from dotenv import load_dotenv

from genai.model import Credentials, Model
from genai.schemas import GenerateParams

# make sure you have a .env file under genai root with
# GENAI_KEY=<your-genai-key>
# GENAI_API=<genai-api-endpoint>
load_dotenv()
api_key = os.getenv("GENAI_KEY", None)
api_endpoint = os.getenv("GENAI_API", None)

# Using Python "with" context
print("\n------------- Example (Greetings)-------------\n")

# Instantiate the GENAI Proxy Object
params = GenerateParams(
decoding_method="sample",
max_new_tokens=10,
min_new_tokens=1,
stream=False,
temperature=0.7,
top_k=50,
top_p=1,
)

# creds object
creds = Credentials(api_key, api_endpoint)
# model object
model = Model("google/flan-ul2", params=params, credentials=creds)

greeting = "Hello! How are you?"
lots_of_greetings = [greeting] * 1000
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.generate_async(lots_of_greetings):
if result is not None:
num_said_greetings += 1
print(f"[Progress {str(float(num_said_greetings/num_of_greetings)*100)}%]")
print(f"\t {result.input_text} --> {result.generated_text}")

```

If you are planning on sending a large number of prompts _and_ using logging, you might want to re-direct genai logs to a file instead of stdout.
Check the section [Tips and TroubleShooting](#tips-and-troubleshooting) for further help.

### <a name='SynchronousExample'></a>Synchronous Example

```python
import os

from dotenv import load_dotenv

from genai.model import Credentials, Model
from genai.schemas import GenerateParams

# make sure you have a .env file under genai root with
# GENAI_KEY=<your-genai-key>
# GENAI_API=<genai-api-endpoint>
load_dotenv()
api_key = os.getenv("GENAI_KEY", None)
api_endpoint = os.getenv("GENAI_API", None)

# Using Python "with" context
print("\n------------- Example (Greetings)-------------\n")

# Instantiate the GENAI Proxy Object
params = GenerateParams(
decoding_method="sample",
max_new_tokens=10,
min_new_tokens=1,
stream=False,
temperature=0.7,
top_k=50,
top_p=1,
)

# creds object
creds = Credentials(api_key, api_endpoint)
# model object
model = Model("google/flan-ul2", params=params, credentials=creds)

greeting1 = "Hello! How are you?"
greeting2 = "I am fine and you?"

# Call generate function
responses = model.generate_as_completed([greeting1, greeting2] * 4)
for response in responses:
print(f"Generated text: {response.generated_text}")

```

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

### <a name='EnablingLogs'></a>Enabling Logs

If you're building an application or example and would like to see the GENAI logs, you can enable them in the following way:

```python
import logging
import os

# Most GENAI logs are at Debug level.
logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"))
```

If you only want genai logs, or those logs at a specific level, you can set this using the following syntax:

```python
logging.getLogger("genai").setLevel(logging.DEBUG)
```

Example log message from GENAI:

```log
DEBUG:genai.model:Model Created: Model: google/flan-t5-xxl, endpoint: https://workbench-api.res.ibm.com/v1/
```

Example of directing genai logs to a file:

```python
# create file handler which logs even debug messages
fh = logging.FileHandler('genai.log')
fh.setLevel(logging.DEBUG)
logging.getLogger("genai").addHandler(fh)
```

To learn more about logging in python, you can follow the tutorial [here](https://docs.python.org/3/howto/logging.html).

### <a name='ManyPrompts'></a>Experimenting with a Large Number of Prompts

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
to work with:
- Start with a small number of prompts to prototype the code. You can enable logging as described above for debugging during prototyping.
- Include exception handling in sensitive sections such as callbacks.
- Checkpoint/save prompts and received responses periodically.
- Check examples in `examples/user` directory and modify them for your needs.

```python
def my_callback(result):
try:
...
except:
...

outputs = []
count = 0
for result in model.generate_async(prompts, callback=my_callback):
if result is not None:
print(result.input_text, " --> ", result.generated_text)
# check if prompts[count] and result.input_text are the same
outputs.append((result.input_text, result.generated_text))
# periodically save outputs to disk or some location
...
else:
# ... save failed prompts for retrying
count += 1
```

## <a name='Extensions'></a>Extensions

GenAI currently supports a langchain extension and more extensions are in the pipeline. Please reach out to
us if you want support for some framework as an extension or want to design an extension yourself.

### <a name='LangChainExtension'></a>LangChain Extension

Install the langchain extension as follows:
```bash
pip install "ibm-generative-ai[langchain]"
```
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
```python
import os
from dotenv import load_dotenv
import genai.extensions.langchain
from genai.extensions.langchain import LangChainInterface
from genai.schemas import GenerateParams
from genai import Credentials, Model, PromptPattern

load_dotenv()
api_key = os.getenv("GENAI_KEY", None)
api_endpoint = os.getenv("GENAI_API", None)
creds = Credentials(api_key, api_endpoint)
params = GenerateParams(decoding_method="greedy")

# As LangChain Model
langchain_model = LangChainInterface(model="google/flan-ul2", params=params, credentials=creds)
print(langchain_model("Answer this question: What is life?"))

# As GenAI Model
genai_model = Model(model="google/flan-ul2", params=params, credentials=creds)
print(genai_model.generate(["Answer this question: What is life?"])[0].generated_text)

# GenAI prompt pattern to langchain PromptTemplate and vice versa
seed_pattern = PromptPattern.from_str("Answer this question: {{question}}")
template = seed_pattern.langchain.as_template()
pattern = PromptPattern.langchain.from_template(template)

print(langchain_model(template.format(question="What is life?")))
print(genai_model.generate([pattern.sub("question", "What is life?")])[0].generated_text)
```

## <a name='[Deprecated] Model Types'></a>[Deprecated] Model Types

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).

Models can be selected by passing their string id to the Model class as exemplified [here](src/genai/schemas/models.py).


## <a name='Support'></a>Support

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