├── .gitignore ├── .idea └── .gitignore ├── README.md ├── requirements.txt ├── serve.py ├── simplemessage.py ├── simplemessagewithparser.py ├── simplemessagewithparserandchain.py └── simplemessagewithparserchainandtemplates.py /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .venv -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **.env contents** 2 | 3 | OPENAI_API_KEY= 4 | 5 | LANGCHAIN_API_KEY= 6 | 7 | LANGCHAIN_TRACING_V2=true 8 | 9 | LANGCHAIN_PROJECT=SIMPLELLM 10 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | langchain==0.2.10 2 | langchain-community==0.2.9 3 | langchain-core==0.2.22 4 | langchain-openai==0.1.17 5 | langchain-text-splitters==0.2.2 6 | openai==1.59.8 7 | python-dotenv==1.0.1 8 | fastapi==0.111.1 9 | langserve==0.2.2 10 | sse_starlette==2.1.2 11 | -------------------------------------------------------------------------------- /serve.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from typing import List 3 | 4 | from fastapi import FastAPI 5 | from langchain_core.prompts import ChatPromptTemplate 6 | from langchain_core.output_parsers import StrOutputParser 7 | from langchain_openai import ChatOpenAI 8 | from langserve import add_routes 9 | from dotenv import load_dotenv 10 | load_dotenv() 11 | 12 | # 1. Create prompt template 13 | system_template = "Translate the following into {language}:" 14 | prompt_template = ChatPromptTemplate.from_messages([ 15 | ('system', system_template), 16 | ('user', '{text}') 17 | ]) 18 | 19 | # 2. Create model 20 | model = ChatOpenAI() 21 | 22 | # 3. Create parser 23 | parser = StrOutputParser() 24 | 25 | # 4. Create chain 26 | chain = prompt_template | model | parser 27 | 28 | 29 | # 4. App definition 30 | app = FastAPI( 31 | title="LangChain Server", 32 | version="1.0", 33 | description="A simple API server using LangChain's Runnable interfaces", 34 | ) 35 | 36 | # 5. Adding chain route 37 | 38 | add_routes( 39 | app, 40 | chain, 41 | path="/chain", 42 | ) 43 | 44 | if __name__ == "__main__": 45 | import uvicorn 46 | uvicorn.run(app, host="localhost", port=8000) -------------------------------------------------------------------------------- /simplemessage.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from langchain_core.messages import HumanMessage, SystemMessage 4 | 5 | load_dotenv() 6 | model = ChatOpenAI(model="gpt-4") 7 | 8 | messages = [ 9 | SystemMessage(content="Translate the following from English into Italian"), 10 | HumanMessage(content="hi!"), 11 | ] 12 | 13 | if __name__ == "__main__": 14 | response = model.invoke(messages) 15 | print(response.content) -------------------------------------------------------------------------------- /simplemessagewithparser.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from langchain_core.messages import HumanMessage, SystemMessage 4 | from langchain_core.output_parsers import StrOutputParser 5 | 6 | load_dotenv() 7 | model = ChatOpenAI(model="gpt-4") 8 | 9 | messages = [ 10 | SystemMessage(content="Translate the following from English into Italian"), 11 | HumanMessage(content="hi!"), 12 | ] 13 | 14 | parser = StrOutputParser() 15 | 16 | result = model.invoke(messages) 17 | 18 | 19 | 20 | if __name__ == "__main__": 21 | print(parser.invoke(result)) 22 | 23 | -------------------------------------------------------------------------------- /simplemessagewithparserandchain.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from langchain_core.messages import HumanMessage, SystemMessage 4 | from langchain_core.output_parsers import StrOutputParser 5 | 6 | load_dotenv() 7 | model = ChatOpenAI(model="gpt-4") 8 | 9 | messages = [ 10 | SystemMessage(content="Translate the following from English into Italian"), 11 | HumanMessage(content="hi!"), 12 | ] 13 | 14 | parser = StrOutputParser() 15 | 16 | chain = model | parser 17 | 18 | 19 | 20 | if __name__ == "__main__": 21 | print(chain.invoke(messages)) 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /simplemessagewithparserchainandtemplates.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from langchain_core.messages import HumanMessage, SystemMessage 4 | from langchain_core.output_parsers import StrOutputParser 5 | from langchain_core.prompts import ChatPromptTemplate 6 | 7 | 8 | load_dotenv() 9 | model = ChatOpenAI(model="gpt-4") 10 | 11 | system_template = "Translate the following into {language}:" 12 | 13 | prompt_template = ChatPromptTemplate.from_messages( 14 | [("system", system_template), ("user", "{text}")] 15 | ) 16 | 17 | parser = StrOutputParser() 18 | 19 | chain = prompt_template | model | parser 20 | 21 | 22 | if __name__ == "__main__": 23 | print(chain.invoke({"language": "italian", "text": "hi"})) 24 | 25 | 26 | 27 | --------------------------------------------------------------------------------