Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
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
h0rv committed Apr 27, 2024
1 parent 88bade9 commit 6b3837d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 120 deletions.
58 changes: 58 additions & 0 deletions docs/examples/watsonx.md
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
```
56 changes: 36 additions & 20 deletions examples/watsonx/watsonx.py
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
5 changes: 0 additions & 5 deletions instructor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,3 @@
from .client_cohere import from_cohere

__all__ += ["from_cohere"]

if importlib.util.find_spec("ibm_watsonx_ai") is not None:
from .client_watsonx import from_watsonx

__all__ += ["from_watsonx"]
2 changes: 0 additions & 2 deletions instructor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def create_partial(
**kwargs,
) -> Generator[T, None, None]:
assert self.provider != Provider.ANTHROPIC, "Anthropic doesn't support partial"
assert self.provider != Provider.WATSONX, "watsonx doesn't support partial"

kwargs["stream"] = True

Expand All @@ -112,7 +111,6 @@ def create_iterable(
**kwargs,
) -> Iterable[T]:
assert self.provider != Provider.ANTHROPIC, "Anthropic doesn't support iterable"
assert self.provider != Provider.WATSONX, "watsonx doesn't support iterable"

kwargs["stream"] = True
kwargs = self.handle_kwargs(kwargs)
Expand Down
82 changes: 0 additions & 82 deletions instructor/client_watsonx.py

This file was deleted.

3 changes: 0 additions & 3 deletions instructor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Provider(Enum):
GROQ = "groq"
MISTRAL = "mistral"
COHERE = "cohere"
WATSONX = "watsonx"
UNKNOWN = "unknown"


Expand All @@ -46,8 +45,6 @@ def get_provider(base_url: str) -> Provider:
return Provider.MISTRAL
elif "cohere" in str(base_url):
return Provider.COHERE
elif "cloud.ibm.com" in str(base_url):
return Provider.WATSONX
return Provider.UNKNOWN


Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mistralai = { version = "^0.1.8", optional = true }
anthropic = ["anthropic", "xmltodict"]
groq = ["groq"]
cohere = ["cohere"]
test-docs = ["fastapi", "redis", "diskcache", "pandas", "tabulate", "pydantic_extra_types", "litellm", "anthropic", "groq", "cohere", "mistralai", "ibm-watsonx-ai"]
test-docs = ["fastapi", "redis", "diskcache", "pandas", "tabulate", "pydantic_extra_types", "litellm", "anthropic", "groq", "cohere", "mistralai"]
mistralai = ["mistralai"]

[tool.poetry.scripts]
Expand All @@ -63,9 +63,6 @@ mkdocs-redirects = "^1.2.1"
[tool.poetry.group.anthropic.dependencies]
anthropic = "^0.23.1"

[tool.poetry.group.watsonx.dependencies]
ibm-watsonx-ai = { version = "^0.2.6", markers = "python_version >= '3.10' and extra == 'watsonx'" }

[tool.poetry.group.test-docs.dependencies]
fastapi = "^0.109.2"
redis = "^5.0.1"
Expand All @@ -83,4 +80,4 @@ mistralai = "^0.1.8"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
build-backend = "poetry.core.masonry.api"

0 comments on commit 6b3837d

Please sign in to comment.