forked from instructor-ai/instructor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example Revert "add/update dependencies" This reverts commit 9787dd4. fix: bullet list in README.md (instructor-ai#621) fix: Update README.md (instructor-ai#625) fix typo about Enum 'and' → 'an' (instructor-ai#626) Fix anthropic usage and tools (instructor-ai#622) Revert init changes
- Loading branch information
Showing
8 changed files
with
99 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Structured Outputs with IBM watsonx.ai | ||
|
||
You can use watsonx foundation models for inference using [LiteLLM](https://docs.litellm.ai/docs/providers/watsonx). | ||
|
||
## watsonx.ai | ||
|
||
You will need an API key and the project ID for your watsonx.ai. | ||
|
||
More information at <https://www.ibm.com/products/watsonx-ai>. | ||
|
||
## Example | ||
|
||
```python | ||
import os | ||
|
||
import litellm | ||
from litellm import completion | ||
from pydantic import BaseModel, Field | ||
|
||
import instructor | ||
from instructor import Mode | ||
|
||
litellm.drop_params = True # since watsonx.ai doesn't support `json_mode` | ||
|
||
os.environ["WATSONX_URL"] = "https://us-south.ml.cloud.ibm.com" | ||
os.environ["WATSONX_APIKEY"] = "" | ||
os.environ["WATSONX_PROJECT_ID"] = "" | ||
|
||
|
||
class Company(BaseModel): | ||
name: str = Field(description="name of the company") | ||
year_founded: int = Field(description="year the company was founded") | ||
|
||
|
||
task = """\ | ||
Given the following text, create a Company object: | ||
IBM was founded in 1911 as the Computing-Tabulating-Recording Company (CTR), a holding company of manufacturers of record-keeping and measuring systems. | ||
""" | ||
|
||
client = instructor.from_litellm(completion, mode=Mode.JSON) | ||
|
||
resp = client.chat.completions.create( | ||
model="watsonx/meta-llama/llama-3-8b-instruct", | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": task, | ||
} | ||
], | ||
response_model=Company, | ||
) | ||
|
||
assert isinstance(resp, Company) | ||
assert resp.name == "IBM" | ||
assert resp.year_founded == 1911 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,44 @@ | ||
import os | ||
|
||
from ibm_watsonx_ai.foundation_models import ModelInference as Watsonx | ||
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams | ||
import litellm | ||
from litellm import completion | ||
from pydantic import BaseModel, Field | ||
|
||
import instructor | ||
from instructor import Mode | ||
|
||
litellm.drop_params = True # since watsonx.ai doesn't support `json_mode` | ||
|
||
os.environ["WATSONX_URL"] = "https://us-south.ml.cloud.ibm.com" | ||
os.environ["WATSONX_APIKEY"] = "" | ||
os.environ["WATSONX_PROJECT_ID"] = "" | ||
|
||
|
||
class Company(BaseModel): | ||
name: str = Field(description="name of the company") | ||
year_founded: int = Field(description="year the company was founded") | ||
|
||
api_key = os.environ.get("WATSONX_API_KEY") | ||
project_id = os.environ.get("WATSONX_PROJECT_ID") | ||
instance_url = "https://us-south.ml.cloud.ibm.com" | ||
|
||
wx = Watsonx( | ||
model_id="meta-llama/meta-llama-3-70b-instruct", | ||
credentials={"apikey": api_key, "url": instance_url}, | ||
project_id=project_id, | ||
params={ | ||
GenParams.MAX_NEW_TOKENS: 500, | ||
GenParams.MIN_NEW_TOKENS: 1, | ||
GenParams.DECODING_METHOD: "sample", | ||
GenParams.TEMPERATURE: 0.5, | ||
GenParams.TOP_K: 50, | ||
GenParams.TOP_P: 1, | ||
}, | ||
) | ||
|
||
task = """\ | ||
Given the following text, create a Company object: | ||
IBM was founded in 1911 as the Computing-Tabulating-Recording Company (CTR), a holding company of manufacturers of record-keeping and measuring systems. | ||
""" | ||
|
||
client = instructor.from_litellm(completion, mode=Mode.JSON) | ||
|
||
resp = client.chat.completions.create( | ||
model="watsonx/meta-llama/llama-3-8b-instruct", | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": task, | ||
} | ||
], | ||
response_model=Company, | ||
) | ||
|
||
client = instructor.from_watsonx(wx, mode=instructor.Mode.MD_JSON) | ||
assert isinstance(resp, Company) | ||
assert resp.name == "IBM" | ||
assert resp.year_founded == 1911 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters