mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-28 14:05:02 +00:00
update
This commit is contained in:
parent
6685cd8ed9
commit
4cec6a70b5
@ -36,7 +36,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 26,
|
||||
"id": "dfdf03ba-d6f5-4b1e-86d3-a65c4bc99aa1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -45,13 +45,13 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[2mUsing Python 3.12.0 environment at: /Users/adeel/vectara/langchain/.venv\u001b[0m\n",
|
||||
"\u001b[2K\u001b[2mResolved \u001b[1m1 package\u001b[0m \u001b[2min 191ms\u001b[0m\u001b[0m \u001b[0m\n",
|
||||
"\u001b[2mAudited \u001b[1m1 package\u001b[0m \u001b[2min 0.21ms\u001b[0m\u001b[0m\n"
|
||||
"\u001b[2K\u001b[2mResolved \u001b[1m1 package\u001b[0m \u001b[2min 189ms\u001b[0m\u001b[0m \u001b[0m\n",
|
||||
"\u001b[2mAudited \u001b[1m1 package\u001b[0m \u001b[2min 0.24ms\u001b[0m\u001b[0m\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!uv pip install -U pip && uv pip install -qU langchain-vectara"
|
||||
"!uv pip install -U pip && uv pip install -qU langchain-vectara langgraph"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -158,11 +158,11 @@
|
||||
"corpus_key = os.getenv(\"VECTARA_CORPUS_KEY\")\n",
|
||||
"\n",
|
||||
"add_files_tool = VectaraAddFiles(\n",
|
||||
" name=\"add_files_tool\",\n",
|
||||
" description=\"Upload files about state of the union\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
" )\n",
|
||||
" name=\"add_files_tool\",\n",
|
||||
" description=\"Upload files about state of the union\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"file_obj = File(\n",
|
||||
" file_path=\"../document_loaders/example_data/state_of_the_union.txt\",\n",
|
||||
@ -186,7 +186,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 30,
|
||||
"id": "9ecda054-96a8-4a91-aeae-32006efb1ac8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -196,7 +196,7 @@
|
||||
"'{\\n \"summary\": \"President Biden discussed several key topics in his recent statements. He emphasized the importance of keeping schools open and noted that with a high vaccination rate and reduced hospitalizations, most Americans can safely return to normal activities [1]. He addressed the need to hold social media platforms accountable for their impact on children and called for stronger privacy protections and mental health services [2]. Biden also announced measures against Russia, including preventing its central bank from defending the Ruble and targeting Russian oligarchs\\' assets, as well as closing American airspace to Russian flights [3], [7]. Additionally, he reaffirmed the need to protect women\\'s rights, particularly the right to choose as affirmed in Roe v. Wade [5].\",\\n \"factual_consistency_score\": 0.5415039\\n}'"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -227,12 +227,12 @@
|
||||
"query_str = \"what did Biden say?\"\n",
|
||||
"\n",
|
||||
"vectara_rag_tool = VectaraRAG(\n",
|
||||
" name=\"RAG tool\",\n",
|
||||
" description=\"Get answers about state of the union\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
" config=config,\n",
|
||||
" )\n",
|
||||
" name=\"rag-tool\",\n",
|
||||
" description=\"Get answers about state of the union\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
" config=config,\n",
|
||||
")\n",
|
||||
"vectara_rag_tool.run(query_str)"
|
||||
]
|
||||
},
|
||||
@ -278,12 +278,12 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"search_tool = VectaraSearch(\n",
|
||||
" name=\"Search tool\",\n",
|
||||
" description=\"Search for information about state of the union\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
" search_config=search_config,\n",
|
||||
" )\n",
|
||||
" name=\"Search tool\",\n",
|
||||
" description=\"Search for information about state of the union\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
" search_config=search_config,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"search_tool.run({\"query\": \"What did Biden say?\"})"
|
||||
]
|
||||
@ -323,10 +323,11 @@
|
||||
],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"from langchain_openai.chat_models import ChatOpenAI\n",
|
||||
"\n",
|
||||
"from langchain.prompts import ChatPromptTemplate\n",
|
||||
"from langchain.schema.output_parser import StrOutputParser\n",
|
||||
"from langchain.schema.runnable import RunnableSerializable\n",
|
||||
"from langchain_openai.chat_models import ChatOpenAI\n",
|
||||
"\n",
|
||||
"llm = ChatOpenAI(temperature=0)\n",
|
||||
"\n",
|
||||
@ -341,18 +342,17 @@
|
||||
"\"\"\"\n",
|
||||
"prompt = ChatPromptTemplate.from_template(template)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Create a function to get RAG results\n",
|
||||
"def get_rag_result(query: str) -> str:\n",
|
||||
" result = vectara_rag_tool.run(query)\n",
|
||||
" result_dict = json.loads(result)\n",
|
||||
" return result_dict[\"summary\"]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Create the chain\n",
|
||||
"chain: RunnableSerializable = (\n",
|
||||
" {\"rag_result\": get_rag_result}\n",
|
||||
" | prompt\n",
|
||||
" | llm\n",
|
||||
" | StrOutputParser()\n",
|
||||
" {\"rag_result\": get_rag_result} | prompt | llm | StrOutputParser()\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Run the chain\n",
|
||||
@ -371,99 +371,45 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 31,
|
||||
"id": "09e87ea6-9ecd-4552-8df9-a105ee190d9a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/Users/adeel/vectara/langchain/.venv/lib/python3.12/site-packages/langsmith/client.py:280: LangSmithMissingAPIKeyWarning: API key must be provided when using hosted LangSmith API\n",
|
||||
" warnings.warn(\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||||
"\u001b[32;1m\u001b[1;3mI need to break down this question into three parts: what is an API key, what is a JWT token, and when to use each. Since the RAG tool is not suitable for this type of question, I will use my knowledge to answer it.\n",
|
||||
"Final Answer: An API key is a unique identifier used to authenticate a user, developer, or calling program to an API. It is a way to ensure that the data being accessed is being done so by an authorized party.\n",
|
||||
"\n",
|
||||
"A JWT (JSON Web Token) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted.\n",
|
||||
"\n",
|
||||
"API keys are used for simpler, less sensitive data, while JWT tokens are used for more sensitive data and when you need to ensure the integrity of the information. JWT tokens also contain information (claims) about the user or system, which can be verified and trusted because it is digitally signed.\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'An API key is a unique identifier used to authenticate a user, developer, or calling program to an API. It is a way to ensure that the data being accessed is being done so by an authorized party.\\n\\nA JWT (JSON Web Token) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted.\\n\\nAPI keys are used for simpler, less sensitive data, while JWT tokens are used for more sensitive data and when you need to ensure the integrity of the information. JWT tokens also contain information (claims) about the user or system, which can be verified and trusted because it is digitally signed.'"
|
||||
"{'messages': [HumanMessage(content='What is an API key? What is a JWT token? When should I use one or the other?', additional_kwargs={}, response_metadata={}, id='2d0d23c4-ca03-4164-8417-232ce12b47df'),\n",
|
||||
" AIMessage(content=\"An API key and a JWT (JSON Web Token) are both methods used for authentication and authorization in web applications, but they serve different purposes and have different characteristics.\\n\\n### API Key\\n- **Definition**: An API key is a unique identifier used to authenticate a client making requests to an API. It is typically a long string of characters that is passed along with the API request.\\n- **Usage**: API keys are often used for simple authentication scenarios where the client needs to be identified, but there is no need for complex user authentication or session management.\\n- **Security**: API keys can be less secure than other methods because they are often static and can be easily exposed if not handled properly. They should be kept secret and not included in public code repositories.\\n- **When to Use**: Use API keys for server-to-server communication, when you need to track usage, or when you want to restrict access to certain features of an API.\\n\\n### JWT (JSON Web Token)\\n- **Definition**: A JWT is a compact, URL-safe means of representing claims to be transferred between two parties. It consists of three parts: a header, a payload, and a signature. The payload typically contains user information and claims.\\n- **Usage**: JWTs are commonly used for user authentication and authorization in web applications. They allow for stateless authentication, meaning the server does not need to store session information.\\n- **Security**: JWTs can be more secure than API keys because they can include expiration times and can be signed to verify their authenticity. However, if a JWT is compromised, it can be used until it expires.\\n- **When to Use**: Use JWTs when you need to authenticate users, manage sessions, or pass claims between parties securely. They are particularly useful in single-page applications (SPAs) and microservices architectures.\\n\\n### Summary\\n- **API Key**: Best for simple authentication and tracking API usage. Less secure and static.\\n- **JWT**: Best for user authentication and authorization with claims. More secure and supports stateless sessions.\\n\\nIn general, choose the method that best fits your application's security requirements and architecture.\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 436, 'prompt_tokens': 66, 'total_tokens': 502, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_dbaca60df0', 'id': 'chatcmpl-BPZK7UZveFJrGkT3iwjNQ2XHCmbqF', 'finish_reason': 'stop', 'logprobs': None}, id='run-4717221a-cd77-4627-aa34-3ee1b2a3803e-0', usage_metadata={'input_tokens': 66, 'output_tokens': 436, 'total_tokens': 502, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"from langchain import hub\n",
|
||||
"from langchain.agents import AgentExecutor, create_react_agent\n",
|
||||
"\n",
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"from langchain_openai.chat_models import ChatOpenAI\n",
|
||||
"from langchain.prompts import ChatPromptTemplate\n",
|
||||
"from langchain.schema.output_parser import StrOutputParser\n",
|
||||
"from langchain.schema.runnable import RunnableSerializable\n",
|
||||
"from langgraph.prebuilt import create_react_agent\n",
|
||||
"\n",
|
||||
"llm = ChatOpenAI(temperature=0)\n",
|
||||
"\n",
|
||||
"# Set up the tools and LLM\n",
|
||||
"tools = [vectara_rag_tool]\n",
|
||||
"llm = ChatOpenAI(model='gpt-4', temperature=0)\n",
|
||||
"\n",
|
||||
"# Get the ReAct prompt template\n",
|
||||
"prompt = hub.pull(\"hwchase17/react\")\n",
|
||||
"prompt.template = '''\n",
|
||||
"Answer the following question as best you can. \n",
|
||||
"\n",
|
||||
"You have access to the following tools:\n",
|
||||
"\n",
|
||||
"{tools}\n",
|
||||
"\n",
|
||||
"Use the following format:\n",
|
||||
"\n",
|
||||
"Question: the input question you must answer\n",
|
||||
"Thought: you should always think about what to do\n",
|
||||
"Action: the action to take, should be one of [{tool_names}]\n",
|
||||
"Action Input: the input to the action\n",
|
||||
"Observation: the result of the action\n",
|
||||
"\n",
|
||||
"(This Thought/Action/Action Input/Observation can repeat N times)\n",
|
||||
"\n",
|
||||
"Thought: I now know the final answer\n",
|
||||
"Final Answer: the final answer to the original input question\n",
|
||||
"\n",
|
||||
"Instructions:\n",
|
||||
"- If a tool response with an incorrect response, you can rephrase your question and try again.\n",
|
||||
"- Base your response primarily on information provided by tools and not prior knowledge.\n",
|
||||
"- Tools respond better to shorter and more concise queries, so try to break down complex questions into simpler sub-questions.\n",
|
||||
"\n",
|
||||
"Begin!\n",
|
||||
"Question: {input}\n",
|
||||
"Thought:{agent_scratchpad}\n",
|
||||
"'''\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"\n",
|
||||
"# Construct the ReAct agent\n",
|
||||
"agent = create_react_agent(llm, tools, prompt)\n",
|
||||
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n",
|
||||
"agent_executor = create_react_agent(llm, tools)\n",
|
||||
"\n",
|
||||
"# Run the agent\n",
|
||||
"agent_executor.invoke({\"input\": \"What is an API key? What is a JWT token? When should I use one or the other?\"})['output']\n"
|
||||
"question = (\n",
|
||||
" \"What is an API key? What is a JWT token? When should I use one or the other?\"\n",
|
||||
")\n",
|
||||
"input_data = {\"messages\": [HumanMessage(content=question)]}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"agent_executor.invoke(input_data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -501,7 +447,7 @@
|
||||
" description=\"Add new documents about planets\",\n",
|
||||
" vectorstore=vectara,\n",
|
||||
" corpus_key=corpus_key,\n",
|
||||
" )\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Test ingest functionality\n",
|
||||
"texts = [\"Mars is a red planet.\", \"Venus has a thick atmosphere.\"]\n",
|
||||
|
Loading…
Reference in New Issue
Block a user