Skip to content

Commit

Permalink
Code generation app demo (#38)
Browse files Browse the repository at this point in the history
* code gen app

* Updated nb

* Added containerfile, requirements.txt and yaml file

* Updated Readme.md file

* Addressed PR feedback

* Updated prompt

* Updated prompt
  • Loading branch information
suppathak authored Feb 14, 2024
1 parent a5d95ff commit 4899efe
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
35 changes: 35 additions & 0 deletions code-generation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Code Generation

This example will deploy a local code-gen application using a llama.cpp model server and a python app built with langchain.

### Download Model

- **codellama**
- Download URL: `wget https://huggingface.co/TheBloke/CodeLlama-7B-Instruct-GGUF/resolve/main/codellama-7b-instruct.Q4_K_M.gguf`

```
cd ../models
wget <Download URL>
cd ../
```

### Deploy Model Service

To start the model service, refer to [the playground model-service document](../playground/README.md). Deploy the LLM server and volumn mount the model of choice.

```
podman run --rm -it -d \
-p 8001:8001 \
-v Local/path/to/locallm/models:/locallm/models:ro,Z \
-e MODEL_PATH=models/<model-filename> \
-e HOST=0.0.0.0 \
-e PORT=8001 \
playground:image
```

### Build Container Image

Once the model service is deployed, then follow the instruction below to build your container image and run it locally.

- `podman build -t codegen-app code-generation -f code-generation/builds/Containerfile`
- `podman run -it -p 8501:8501 codegen-app -- -m http://10.88.0.1:8001/v1`
20 changes: 20 additions & 0 deletions code-generation/ai-studio.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
application:
type: language
name: codegen-demo
description: This is a code-generation demo application.
containers:
- name: llamacpp-server
contextdir: ../playground
containerfile: Containerfile
model-service: true
backend:
- llama
arch:
- arm64
- amd64
- name: codegen-app
contextdir: .
containerfile: builds/Containerfile
arch:
- arm64
- amd64
9 changes: 9 additions & 0 deletions code-generation/builds/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM registry.access.redhat.com/ubi9/python-39:latest

WORKDIR /code-generation
COPY builds/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir --upgrade -r /code-generation/requirements.txt
COPY codegen-app.py .
EXPOSE 8501
ENTRYPOINT ["streamlit", "run", "codegen-app.py"]
3 changes: 3 additions & 0 deletions code-generation/builds/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
langchain_openai
langchain
streamlit
43 changes: 43 additions & 0 deletions code-generation/codegen-app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_community.callbacks import StreamlitCallbackHandler

import streamlit as st

model_service = os.getenv("MODEL_SERVICE_ENDPOINT", "http://localhost:8001/v1")

st.title("Code Generation App")

if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant",
"content": "How can I help you?"}]

for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])

llm = ChatOpenAI(base_url=model_service,
api_key="EMPTY",
streaming=True)

# Define the Langchain chain
prompt = ChatPromptTemplate.from_template("""You are an helpful code assistant that can help developer to code for a given {input}.
Generate the code block at first, and explain the code at the end.
If the {input} is not making sense, please ask for more clarification.""")
chain = (
{"input": RunnablePassthrough()}
| prompt
| llm
)

if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").markdown(prompt)

st_callback = StreamlitCallbackHandler(st.container())
response = chain.invoke(prompt, {"callbacks": [st_callback]})

st.chat_message("assistant").markdown(response.content)
st.session_state.messages.append({"role": "assistant", "content": response.content})
st.rerun()

0 comments on commit 4899efe

Please sign in to comment.