-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
langchain_openai | ||
langchain | ||
streamlit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |