-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
194 additions
and
0 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,194 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -qU langchain-ibm" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from dotenv import load_dotenv\n", | ||
"load_dotenv()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"parameters = {\n", | ||
" \"decoding_method\": \"sample\",\n", | ||
" \"max_new_tokens\": 100,\n", | ||
" \"min_new_tokens\": 1,\n", | ||
" \"temperature\": 0.5,\n", | ||
" \"top_k\": 50,\n", | ||
" \"top_p\": 1,\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_ibm import WatsonxLLM\n", | ||
"import os\n", | ||
"\n", | ||
"watsonx_llm = WatsonxLLM(\n", | ||
" model_id=\"ibm/granite-13b-instruct-v2\",\n", | ||
" url=\"https://us-south.ml.cloud.ibm.com\",\n", | ||
" apikey=os.getenv(\"IAM_API_KEY\", None),\n", | ||
" project_id=os.getenv(\"PROJECT_ID\"),\n", | ||
" params=parameters,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain.prompts import PromptTemplate\n", | ||
"\n", | ||
"template = \"Generate a random question about {topic}: Question: \"\n", | ||
"prompt = PromptTemplate.from_template(template)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'topic': 'dog',\n", | ||
" 'text': 'What is the largest breed of dog that can be fully grown without any supplements?'}" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from langchain.chains import LLMChain\n", | ||
"\n", | ||
"llm_chain = LLMChain(prompt=prompt, llm=watsonx_llm)\n", | ||
"llm_chain.invoke(\"dog\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"\"Man's best friend is his dog. Dogs are man's best friend because they are always there for you, they never judge you, and they always want to play. Dogs are also very smart, and they can learn tricks like coming when called or sitting up on two legs. Dogs are the best because they are always happy to see you and they make you feel better when you are sad. \"" | ||
] | ||
}, | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Calling a single prompt\n", | ||
"\n", | ||
"watsonx_llm.invoke(\"Who is man's best friend?\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"LLMResult(generations=[[Generation(text='The fastest dog in the world is the Greyhound. Greyhounds can reach speeds of up to 45 mph. They are very fast because they have long legs, a streamlined body, and a powerful tail. ', generation_info={'finish_reason': 'eos_token'})], [Generation(text='The Labrador Retriever is a very popular dog breed. They are known for their friendly personalities and their love of water. Labradors are great family dogs and are very trainable. They are also very intelligent and make great working dogs. ', generation_info={'finish_reason': 'eos_token'})]], llm_output={'token_usage': {'generated_token_count': 99, 'input_token_count': 13}, 'model_id': 'ibm/granite-13b-instruct-v2', 'deployment_id': ''}, run=[RunInfo(run_id=UUID('cd217963-8f56-427c-a649-daff87086bc2')), RunInfo(run_id=UUID('75ef8f97-1cce-4338-b7a3-aaa820a9c7c4'))])" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Calling multiple prompts\n", | ||
"\n", | ||
"watsonx_llm.generate(\n", | ||
" [\n", | ||
" \"The fastest dog in the world?\",\n", | ||
" \"Describe your chosen dog breed\",\n", | ||
" ]\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"My favorite breed of dog is a Labrador Retriever. Labradors are the most popular dog breed in the United States. They are friendly, playful, and easy to train. They are also very loyal and make great companions. " | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for chunk in watsonx_llm.stream(\n", | ||
" \"Describe your favorite breed of dog and why it is your favorite.\"\n", | ||
"):\n", | ||
" print(chunk, end=\"\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "new-trace", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |