Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
This package provides the integration between LangChain and IBM watsonx.ai through the ibm-watsonx-ai
SDK.
To use the langchain-ibm
package, follow these installation steps:
pip install langchain-ibm
To use IBM's models, you must have an IBM Cloud user API key. Here's how to obtain and set up your API key:
import os
from getpass import getpass
watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key
In alternative, you can set the environment variable in your terminal.
Linux/macOS: Open your terminal and execute the following command:
export WATSONX_APIKEY='your_ibm_api_key'
To make this environment variable persistent across terminal sessions, add the above line to your ~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
file.
Windows: For Command Prompt, use:
set WATSONX_APIKEY=your_ibm_api_key
You might need to adjust model parameters for different models or tasks. For more details on the parameters, refer to IBM's documentation.
parameters = {
"decoding_method": "sample",
"max_new_tokens": 100,
"min_new_tokens": 1,
"temperature": 0.5,
"top_k": 50,
"top_p": 1,
}
Initialize the WatsonxLLM class with the previously set parameters.
from langchain_ibm import WatsonxLLM
watsonx_llm = WatsonxLLM(
model_id="PASTE THE CHOSEN MODEL_ID HERE",
url="PASTE YOUR URL HERE",
project_id="PASTE YOUR PROJECT_ID HERE",
params=parameters,
)
Note:
project_id
or space_id
. For more information refer to IBM's documentation.model_id
. You can find the list of available models here.Alternatively you can use Cloud Pak for Data credentials. For more details, refer to IBM's documentation.
watsonx_llm = WatsonxLLM(
model_id="ibm/granite-13b-instruct-v2",
url="PASTE YOUR URL HERE",
username="PASTE YOUR USERNAME HERE",
password="PASTE YOUR PASSWORD HERE",
instance_id="openshift",
version="4.8",
project_id="PASTE YOUR PROJECT_ID HERE",
params=parameters,
)
Create PromptTemplate
objects which will be responsible for creating a random question.
from langchain_core.prompts import PromptTemplate
template = "Generate a random question about {topic}: Question: "
prompt = PromptTemplate.from_template(template)
Provide a topic and run the LLMChain.
from langchain_core.output_parsers import StrOutputParser
llm_chain = prompt | watsonx_llm | StrOutputParser()
topic = "dog"
llm_chain.invoke(topic)
print(response)
To obtain completions, you can call the model directly using a string prompt.
# Calling a single prompt
response = watsonx_llm.invoke("Who is man's best friend?")
print(response)
# Calling multiple prompts
response = watsonx_llm.generate(
[
"The fastest dog in the world?",
"Describe your chosen dog breed",
]
)
print(response)
You can stream the model output.
for chunk in watsonx_llm.stream(
"Describe your favorite breed of dog and why it is your favorite."
):
print(chunk, end="", flush=True)
FAQs
An integration package connecting IBM watsonx.ai and LangChain
We found that langchain-ibm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.