Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

04_entity_extraction: Replace deprecated langchain API #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions 01_Text_generation/04_entity_extraction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"outputs": [],
"source": [
"%pip install langchain==0.1.11"
"%pip install langchain-aws\n",
"%pip install bs4"
]
},
{
Expand All @@ -56,7 +57,7 @@
"import boto3\n",
"import botocore\n",
"\n",
"boto3_bedrock = boto3.client('bedrock-runtime')"
"boto3_bedrock = boto3.client(\"bedrock-runtime\")"
]
},
{
Expand Down Expand Up @@ -84,18 +85,19 @@
},
"outputs": [],
"source": [
"from langchain.llms.bedrock import Bedrock\n",
"from langchain_aws import BedrockLLM\n",
"\n",
"# - create the Anthropic Model\n",
"llm = Bedrock(\n",
"llm = BedrockLLM(\n",
" model_id=\"anthropic.claude-v2\",\n",
" client=boto3_bedrock,\n",
" model_kwargs={\n",
" \"max_tokens_to_sample\": 200,\n",
" \"temperature\": 0, # Using 0 to get reproducible results\n",
" \"stop_sequences\": [\"\\n\\nHuman:\"]\n",
" }\n",
")"
" \"temperature\": 0, # Using 0 to get reproducible results\n",
" \"stop_sequences\": [\"\\n\\nHuman:\"],\n",
" },\n",
")\n",
"\n",
"print(f\"llm:\\n{llm}\")"
]
},
{
Expand Down Expand Up @@ -171,7 +173,7 @@
},
"outputs": [],
"source": [
"result = llm(query)\n",
"result = llm.invoke(input=query)\n",
"print(result.strip())"
]
},
Expand Down Expand Up @@ -223,7 +225,7 @@
"outputs": [],
"source": [
"query = prompt.format(email=book_question_email)\n",
"result = llm(query)\n",
"result = llm.invoke(input=query)\n",
"print(result.strip())"
]
},
Expand All @@ -246,12 +248,13 @@
"source": [
"from bs4 import BeautifulSoup\n",
"\n",
"\n",
"def extract_by_tag(response: str, tag: str, extract_all=False) -> str | list[str] | None:\n",
" soup = BeautifulSoup(response)\n",
" results = soup.find_all(tag)\n",
" if not results:\n",
" return\n",
" \n",
"\n",
" texts = [res.get_text() for res in results]\n",
" if extract_all:\n",
" return texts\n",
Expand Down Expand Up @@ -303,7 +306,7 @@
"outputs": [],
"source": [
"query = prompt.format(email=return_email)\n",
"result = llm(query)\n",
"result = llm.invoke(input=query)\n",
"print(result.strip())"
]
},
Expand Down Expand Up @@ -359,7 +362,7 @@
"outputs": [],
"source": [
"query = prompt.format(email=book_question_email)\n",
"result = llm(query)\n",
"result = llm.invoke(input=query)\n",
"print(result.strip())"
]
},
Expand Down