Documentation Index
Fetch the complete documentation index at: https://docs.morph-data.io/llms.txt
Use this file to discover all available pages before exploring further.
Building Your First Morph App
As your first AI app using Morph, we will create a chat app using Langchain.
This tutorial requires an OpenAI API key. To obtain an API key, please visit the OpenAI website.
Initialize the Project
Run the following command in the terminal to create a new project. Install Packages
To use Langchain, run the following command to install the package.pip install langchain langchain-openai
Create chat.py
Create a chat.py file in the src/python directory.import morph
from morph import MorphGlobalContext
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
@morph.func
def langchain_chat(context: MorphGlobalContext):
llm = ChatOpenAI(model="gpt-4o")
messages = [HumanMessage(context.vars["prompt"])]
for token in llm.stream(messages):
yield token.content
Edit index.mdx
Edit index.mdx to use the Python function created earlier.# 🦜🔗 Langchain Chat
<Chat postData="langchain_chat" height={300} />
Add API key to .env
Add your OpenAI API key to the .env file.OPENAI_API_KEY=your_api_key
Start the Local Server
Run the following command in the terminal to start the development server.Access localhost:8080 to open the app.
Here is all the code needed to build a simple chat app. That’s it!
import morph
from morph import MorphGlobalContext
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
@morph.func
def langchain_chat(context: MorphGlobalContext):
llm = ChatOpenAI(model="gpt-4o")
messages = [HumanMessage(context.vars["prompt"])]
for token in llm.stream(messages):
yield token.content