{"id":1384,"date":"2024-09-26T22:43:05","date_gmt":"2024-09-26T14:43:05","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1384"},"modified":"2024-09-26T23:37:40","modified_gmt":"2024-09-26T15:37:40","slug":"langchain-quick-start","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1384","title":{"rendered":"langchain quick start"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>LangChain is a framework to build with LLMs by chaining interoperable components. LangGraph is the framework for building controllable agentic workflows.<\/p>\n<h2>build an application<\/h2>\n<ul>\n<li>install library\n<pre><code class=\"language-shell\">pip install langchain\npip install -qU langchain-openai<\/code><\/pre>\n<h3>example 1<\/h3>\n<\/li>\n<\/ul>\n<pre><code class=\"language-python\">#!\/usr\/bin\/env python3\nfrom typing import List\nfrom langchain_openai import ChatOpenAI\nfrom langchain_core.output_parsers import StrOutputParser\nfrom langchain_core.prompts import ChatPromptTemplate\n\nimport os\nfrom dotenv import load_dotenv\nload_dotenv()\n\n#os.environ[&quot;LANGCHAIN_TRACING_V2&quot;] = &quot;true&quot;\nos.environ[&quot;OPENAI_API_KEY&quot;] = os.getenv(&quot;LLM_API_KEY&quot;)\nos.environ[&quot;USER_AGENT&quot;] = &quot;waltertest&quot;\n#os.environ[&quot;LANGCHAIN_API_KEY&quot;] = getpass.getpass()\n#os.environ[&quot;OPENAI_API_KEY&quot;] = getpass.getpass()\n\nmodel = ChatOpenAI(\n    model=&#039;deepseek-chat&#039;,\n    openai_api_key=os.getenv(&quot;LLM_API_KEY&quot;),\n    openai_api_base=os.getenv(&quot;LLM_BASE_URL&quot;),\n    max_tokens=4096\n)\n\nsystem_template = &quot;Translate the following into {language}:&quot;\nprompt_template = ChatPromptTemplate.from_messages(\n    [(&quot;system&quot;, system_template), (&quot;user&quot;, &quot;{text}&quot;)]\n)\n\nparser = StrOutputParser()\n\n#result = model.invoke(messages)\n#parser.invoke(result)\n#result = prompt_template.invoke({&quot;language&quot;: &quot;Chinese&quot;, &quot;text&quot;: &quot;hi&quot;})\n\nchain = prompt_template | model | parser\n\nresult = chain.invoke({&quot;language&quot;: &quot;chinese&quot;, &quot;text&quot;: &quot;As you sow, so shall you reap&quot;})\nprint(result)<\/code><\/pre>\n<p>\u8f93\u51fa: \u79cd\u74dc\u5f97\u74dc\uff0c\u79cd\u8c46\u5f97\u8c46<\/p>\n<h3>example 2<\/h3>\n<pre><code class=\"language-python\">#!\/usr\/bin\/env python\nfrom fastapi import FastAPI\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_core.output_parsers import StrOutputParser\nfrom langchain_openai import ChatOpenAI\nfrom langserve import add_routes\n\nimport os, sys\nfrom loguru import logger\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\n# 1. Create prompt template\nsystem_template = &quot;Translate the following into {language}:&quot;\nprompt_template = ChatPromptTemplate.from_messages([\n    (&#039;system&#039;, system_template),\n    (&#039;user&#039;, &#039;{text}&#039;)\n])\n\n# 2. Create model\nmodel = ChatOpenAI(\n    model=&#039;deepseek-chat&#039;,\n    openai_api_key=os.getenv(&quot;LLM_API_KEY&quot;),\n    openai_api_base=os.getenv(&quot;LLM_BASE_URL&quot;),\n    max_tokens=4096\n)\n\n# 3. Create parser\nparser = StrOutputParser()\n\n# 4. Create chain\nchain = prompt_template | model | parser\n\n# 4. App definition\napp = FastAPI(\n  title=&quot;LangChain Server&quot;,\n  version=&quot;1.0&quot;,\n  description=&quot;A simple API server using LangChain&#039;s Runnable interfaces&quot;,\n)\n\n# 5. Adding chain route\nadd_routes(\n    app,\n    chain,\n    path=&quot;\/chain&quot;,\n)\n\nif __name__ == &quot;__main__&quot;:\n    import uvicorn\n\n    uvicorn.run(app, host=&quot;localhost&quot;, port=8000)<\/code><\/pre>\n<ul>\n<li>Call the above API<\/li>\n<\/ul>\n<pre><code class=\"language-python\">#!\/usr\/bin\/env python\nfrom langserve import RemoteRunnable\n\nremote_chain = RemoteRunnable(&quot;http:\/\/localhost:8000\/chain\/&quot;)\nresult = remote_chain.invoke({&quot;language&quot;: &quot;chinese&quot;, &quot;text&quot;: &quot;As you sow, so shall you reap&quot;})\nprint(result)<\/code><\/pre>\n<p>\u6253\u5370: \u79cd\u74dc\u5f97\u74dc\uff0c\u79cd\u8c46\u5f97\u8c46<\/p>\n<h2>Reference<\/h2>\n<ul>\n<li><a href=\"https:\/\/python.langchain.com\/v0.2\/docs\/tutorials\/llm_chain\/\">https:\/\/python.langchain.com\/v0.2\/docs\/tutorials\/llm_chain\/<\/a><\/li>\n<li><a href=\"https:\/\/python.langchain.com\/docs\/concepts\/\">https:\/\/python.langchain.com\/docs\/concepts\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview LangChain is a framework to build with LLMs by chaining interoperable components. LangGraph is the framework for building controllable agentic workflows. build an application install library pip install langchain pip install -qU langchain-openai example 1 #!\/usr\/bin\/env python3 from typing import List from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate import [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1384\" title=\"Permanent Link to: langchain quick start\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1384","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1384"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1384"}],"version-history":[{"count":10,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":1394,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1384\/revisions\/1394"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}