|
| 1 | +# Watsonx Usage Guide |
| 2 | + |
| 3 | +This guide provides instructions on how to use the `Watsonx` class to interact with IBM Watsonx.ai API for text and code generation. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Python 3.10 or higher. |
| 8 | +- The `ibm-watsonx-ai` package installed, which can be installed via pip. |
| 9 | +- An IBM Cloud account and a Watsonx configured project. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Ensure you have installed the `ibm-watsonx-ai` package along with other necessary dependencies: |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Before using the `Watsonx` class, you need to set up access to IBM Cloud: |
| 18 | + |
| 19 | +1. Create an IBM Cloud account |
| 20 | +2. Enable a Watsonx service from the catalog |
| 21 | +3. Create a new project and associate a Watson Machine Learning service instance. |
| 22 | +4. Create an IAM authentication credentials and save them in a JSON file. |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +Here's an example of how to instantiate the `Watsonx` class and send a generation request: |
| 27 | + |
| 28 | +```python |
| 29 | +import dspy |
| 30 | + |
| 31 | +''' Initialize the class with the model name and parameters for Watsonx.ai |
| 32 | + You can choose between many different models: |
| 33 | + * (Mistral) mistralai/mixtral-8x7b-instruct-v01 |
| 34 | + * (Meta) meta-llama/llama-3-70b-instruct |
| 35 | + * (IBM) ibm/granite-13b-instruct-v2 |
| 36 | + * and many others. |
| 37 | +''' |
| 38 | +watsonx=dspy.Watsonx( |
| 39 | + model='mistralai/mixtral-8x7b-instruct-v01', |
| 40 | + credentials={ |
| 41 | + "apikey": "your-api-key", |
| 42 | + "url": "https://us-south.ml.cloud.ibm.com" |
| 43 | + }, |
| 44 | + project_id="your-watsonx-project-id", |
| 45 | + max_new_tokens=500, |
| 46 | + max_tokens=1000 |
| 47 | + ) |
| 48 | + |
| 49 | +dspy.settings.configure(lm=watsonx) |
| 50 | +``` |
| 51 | + |
| 52 | +## Customizing Requests |
| 53 | + |
| 54 | +You can customize requests by passing additional parameters such as `decoding_method`,`max_new_tokens`, `stop_sequences`, `repetition_penalty`, and others supported by the Watsonx.ai API. This allows you to control the behavior of the generation. |
| 55 | +Refer to [`ibm-watsonx-ai library`](https://ibm.github.io/watsonx-ai-python-sdk/index.html) documentation. |
0 commit comments