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

community: SamabaStudio Tool Calling and Structured Output #28025

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
d9ae887
Add SambaNovaCloud bind tools
jhpiedrahitao Nov 6, 2024
519f0c1
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 6, 2024
4e73194
fmt
jhpiedrahitao Nov 7, 2024
f2d3719
minor changes typing
jhpiedrahitao Nov 7, 2024
93543b8
add tool calling in docs
jhpiedrahitao Nov 7, 2024
f6a53ef
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 7, 2024
6050c7c
fmt
jhpiedrahitao Nov 7, 2024
47a58cb
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 7, 2024
8ec7ab3
minor change
jhpiedrahitao Nov 7, 2024
d4b50f4
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 7, 2024
d833044
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 8, 2024
15a4c2b
Merge branch 'snova-jorgep/bind_tools_sambanova_chat_models' into sno…
jhpiedrahitao Nov 8, 2024
e419b01
add bind tools sambastudio chat model
jhpiedrahitao Nov 8, 2024
89a474c
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 11, 2024
fdf5cc9
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 11, 2024
f7731ba
add tool calling to samabstudio chat model docs
jhpiedrahitao Nov 11, 2024
759836b
minor renaming CoE convention to bundle
jhpiedrahitao Nov 11, 2024
89f5ae5
fmt
jhpiedrahitao Nov 11, 2024
b3d87fb
fmt
jhpiedrahitao Nov 11, 2024
e3c8e0b
minor change in mesagges dicts
jhpiedrahitao Nov 13, 2024
1747072
minor changes
jhpiedrahitao Nov 13, 2024
80bd8f2
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 13, 2024
23415df
Merge branch 'snova-jorgep/bind_tools_sambanova_chat_models' into sno…
jhpiedrahitao Nov 13, 2024
1be9884
fix typo
jhpiedrahitao Nov 13, 2024
5ed332b
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 13, 2024
5b40dbe
Merge branch 'snova-jorgep/bind_tools_sambanova_chat_models' into sno…
jhpiedrahitao Nov 13, 2024
43ae94d
minor changes
jhpiedrahitao Nov 13, 2024
7b53800
fmt
jhpiedrahitao Nov 13, 2024
2e1b46b
add structured output
jhpiedrahitao Nov 13, 2024
c9a87a1
add structured output and image examples to integrations docs
jhpiedrahitao Nov 13, 2024
faacb10
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 13, 2024
065334e
updated docs links
jhpiedrahitao Nov 13, 2024
b659399
Merge remote-tracking branch 'refs/remotes/origin/snova-jorgep/bind_t…
jhpiedrahitao Nov 13, 2024
9c9e708
Merge branch 'snova-jorgep/bind_tools_sambanova_chat_models' into sno…
jhpiedrahitao Nov 13, 2024
7b14634
fmt docstrings
jhpiedrahitao Nov 13, 2024
ba4c22c
fmt docstrings
jhpiedrahitao Nov 13, 2024
55734ab
fmt docstrings
jhpiedrahitao Nov 13, 2024
8a20815
Merge branch 'snova-jorgep/bind_tools_sambanova_chat_models' into sno…
jhpiedrahitao Nov 14, 2024
224710a
add structured output sambastudio chat model
jhpiedrahitao Nov 14, 2024
c3ce0d1
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
jhpiedrahitao Nov 14, 2024
59d95a2
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 14, 2024
b346fb0
add structured output sambastudio chat model docs notebook
jhpiedrahitao Nov 14, 2024
28e1a7e
fix typo
jhpiedrahitao Nov 15, 2024
35a29a9
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 15, 2024
e55c580
fmt
jhpiedrahitao Nov 18, 2024
63da623
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 18, 2024
bc12f3e
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 18, 2024
d048ccc
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 18, 2024
2d722e4
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 18, 2024
a97bd1a
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 18, 2024
2897b4e
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 19, 2024
16d5486
Merge branch 'master' into snova-jorgep/bind_tools_sambanova_chat_models
jhpiedrahitao Nov 19, 2024
dde001b
Merge branch 'snova-jorgep/bind_tools_sambanova_chat_models' into sno…
jhpiedrahitao Nov 19, 2024
8781a4c
tool callig samabstudio updated
jhpiedrahitao Nov 19, 2024
68515a3
fmt
jhpiedrahitao Nov 19, 2024
1535964
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
jhpiedrahitao Nov 19, 2024
4f7039f
fix typo
jhpiedrahitao Nov 20, 2024
26e04b2
Merge branch 'langchain-ai:master' into snova-jorgep/bind_tools_samba…
jhpiedrahitao Nov 20, 2024
f91a989
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
jhpiedrahitao Nov 20, 2024
de5ded1
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
jhpiedrahitao Nov 22, 2024
c9900b6
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
efriis Dec 4, 2024
26810c7
minor changes
jhpiedrahitao Dec 5, 2024
166a662
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
jhpiedrahitao Dec 5, 2024
6bf0415
Merge branch 'master' into snova-jorgep/bind_tools_sambastudio_chat_m…
efriis Dec 16, 2024
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
136 changes: 132 additions & 4 deletions docs/docs/integrations/chat/sambastudio.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| | | | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | \n",
"| | | | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | \n",
"\n",
"## Setup\n",
"\n",
Expand Down Expand Up @@ -119,20 +119,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.chat_models.sambanova import ChatSambaStudio\n",
"\n",
"llm = ChatSambaStudio(\n",
" model=\"Meta-Llama-3-70B-Instruct-4096\", # set if using a CoE endpoint\n",
" model=\"Meta-Llama-3-70B-Instruct-4096\", # set if using a Bundle endpoint\n",
" max_tokens=1024,\n",
" temperature=0.7,\n",
" top_k=1,\n",
" top_p=0.01,\n",
" do_sample=True,\n",
" process_prompt=\"True\", # set if using a CoE endpoint\n",
" process_prompt=\"True\", # set if using a Bundle endpoint\n",
")"
]
},
Expand Down Expand Up @@ -349,6 +349,134 @@
" print(chunk.content, end=\"\", flush=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tool calling"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage\n",
"from langchain_core.tools import tool\n",
"\n",
"\n",
"@tool\n",
"def get_time(kind: str = \"both\") -> str:\n",
" \"\"\"Returns current date, current time or both.\n",
" Args:\n",
" kind: date, time or both\n",
" \"\"\"\n",
" if kind == \"date\":\n",
" date = datetime.now().strftime(\"%m/%d/%Y\")\n",
" return f\"Current date: {date}\"\n",
" elif kind == \"time\":\n",
" time = datetime.now().strftime(\"%H:%M:%S\")\n",
" return f\"Current time: {time}\"\n",
" else:\n",
" date = datetime.now().strftime(\"%m/%d/%Y\")\n",
" time = datetime.now().strftime(\"%H:%M:%S\")\n",
" return f\"Current date: {date}, Current time: {time}\"\n",
"\n",
"\n",
"tools = [get_time]\n",
"\n",
"\n",
"def invoke_tools(tool_calls, messages):\n",
" available_functions = {tool.name: tool for tool in tools}\n",
" for tool_call in tool_calls:\n",
" selected_tool = available_functions[tool_call[\"name\"]]\n",
" tool_output = selected_tool.invoke(tool_call[\"args\"])\n",
" print(f\"Tool output: {tool_output}\")\n",
" messages.append(ToolMessage(tool_output, tool_call_id=tool_call[\"id\"]))\n",
" return messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"llm_with_tools = llm.bind_tools(tools=tools)\n",
"messages = [\n",
" HumanMessage(\n",
" content=\"I need to schedule a meeting for two weeks from today. Can you tell me the exact date of the meeting?\"\n",
" )\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Intermediate model response: [{'name': 'get_time', 'args': {'kind': 'date'}, 'id': 'call_4092d5dd21cd4eb494', 'type': 'tool_call'}]\n",
"Tool output: Current date: 11/07/2024\n",
"final response: The meeting will be exactly two weeks from today, which would be 25/07/2024.\n"
]
}
],
"source": [
"response = llm_with_tools.invoke(messages)\n",
"while len(response.tool_calls) > 0:\n",
" print(f\"Intermediate model response: {response.tool_calls}\")\n",
" messages.append(response)\n",
" messages = invoke_tools(response.tool_calls, messages)\n",
"response = llm_with_tools.invoke(messages)\n",
"\n",
"print(f\"final response: {response.content}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Structured Outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Joke(setup='Why did the cat join a band?', punchline='Because it wanted to be the purr-cussionist!')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from pydantic import BaseModel, Field\n",
"\n",
"\n",
"class Joke(BaseModel):\n",
" \"\"\"Joke to tell user.\"\"\"\n",
"\n",
" setup: str = Field(description=\"The setup of the joke\")\n",
" punchline: str = Field(description=\"The punchline to the joke\")\n",
"\n",
"\n",
"structured_llm = llm.with_structured_output(Joke)\n",
"\n",
"structured_llm.invoke(\"Tell me a joke about cats\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading
Loading