Skip to content

Commit

Permalink
Complete the LLM deployment (#139)
Browse files Browse the repository at this point in the history
* make deployed version work

* set 4o as the new default

* formatting
  • Loading branch information
strickvl authored Oct 28, 2024
1 parent d259599 commit e82b37b
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 51 deletions.
2 changes: 1 addition & 1 deletion llm-complete-guide/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
RATE_LIMIT = 5 # Maximum number of requests per second

# LLM Utils constants
OPENAI_MODEL = "gpt-3.5-turbo"
OPENAI_MODEL = "gpt-4o"
EMBEDDINGS_MODEL = "sentence-transformers/all-MiniLM-L12-v2"
MODEL_NAME_MAP = {
"gpt4": "gpt-4",
Expand Down
2 changes: 0 additions & 2 deletions llm-complete-guide/most_basic_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import string

from openai import OpenAI

from utils.openai_utils import get_openai_api_key


Expand Down
2 changes: 0 additions & 2 deletions llm-complete-guide/most_basic_rag_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
# limitations under the License.
#

import os
import re
import string

from openai import OpenAI

from utils.openai_utils import get_openai_api_key


Expand Down
14 changes: 7 additions & 7 deletions llm-complete-guide/notebooks/argilla_embeddings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
"source": [
"import os\n",
"\n",
"from distilabel.steps.tasks import GenerateSentencePair\n",
"from distilabel.llms import OpenAILLM, OllamaLLM\n",
"from distilabel.steps import LoadDataFromHub\n",
"from distilabel.llms import OpenAILLM\n",
"from distilabel.pipeline import Pipeline\n",
"from distilabel.steps import LoadDataFromHub\n",
"from distilabel.steps.tasks import GenerateSentencePair\n",
"\n",
"# TODO: I think we might optimize this a bit more.\n",
"\n",
Expand Down Expand Up @@ -254,8 +254,8 @@
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer\n",
"import torch\n",
"from sentence_transformers import SentenceTransformer\n",
"\n",
"model_id = \"Snowflake/snowflake-arctic-embed-m\" # Hugging Face model ID\n",
"\n",
Expand Down Expand Up @@ -508,8 +508,8 @@
"outputs": [],
"source": [
"import argilla as rg\n",
"from zenml.client import Client\n",
"from argilla._exceptions import ConflictError\n",
"from zenml.client import Client\n",
"\n",
"zenml_client = Client()\n",
"api_key = zenml_client.get_secret(\"argilla_hf\").secret_values[\"api_key\"]\n",
Expand Down Expand Up @@ -654,13 +654,13 @@
"outputs": [],
"source": [
"import torch\n",
"from datasets import concatenate_datasets, load_dataset\n",
"from sentence_transformers import SentenceTransformer\n",
"from sentence_transformers.evaluation import (\n",
" InformationRetrievalEvaluator,\n",
" SequentialEvaluator,\n",
")\n",
"from sentence_transformers.util import cos_sim\n",
"from datasets import load_dataset, concatenate_datasets\n",
"\n",
"model_id = \"Snowflake/snowflake-arctic-embed-m\" # Hugging Face model ID\n",
"matryoshka_dimensions = [384, 256, 128, 64] # Important: large to small\n",
Expand Down Expand Up @@ -752,8 +752,8 @@
"outputs": [],
"source": [
"from sentence_transformers import (\n",
" SentenceTransformerModelCardData,\n",
" SentenceTransformer,\n",
" SentenceTransformerModelCardData,\n",
")\n",
"\n",
"model_id = \"Snowflake/snowflake-arctic-embed-m\"\n",
Expand Down
6 changes: 2 additions & 4 deletions llm-complete-guide/notebooks/finetune_embeddings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"outputs": [],
"source": [
"from datasets import Dataset, DatasetDict\n",
"from huggingface_hub import create_repo\n",
"from zenml.client import Client\n",
"\n",
"# Assuming you have a Dataset object named 'dataset'\n",
Expand Down Expand Up @@ -174,9 +173,8 @@
"metadata": {},
"outputs": [],
"source": [
"from torch.utils.data import DataLoader\n",
"from sentence_transformers import losses\n",
"\n",
"from torch.utils.data import DataLoader\n",
"\n",
"train_dataloaderB = DataLoader(train_examplesB, shuffle=True, batch_size=64)\n",
"train_lossB = losses.MultipleNegativesRankingLoss(model=modelB)\n",
Expand Down Expand Up @@ -218,8 +216,8 @@
}
],
"source": [
"from torch.utils.data import Dataset, DataLoader\n",
"from sentence_transformers import InputExample\n",
"from torch.utils.data import DataLoader, Dataset\n",
"\n",
"\n",
"class InputExampleDataset(Dataset):\n",
Expand Down
4 changes: 2 additions & 2 deletions llm-complete-guide/notebooks/visualise_embeddings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
],
"source": [
"import matplotlib.pyplot as plt\n",
"from sklearn.manifold import TSNE\n",
"import umap\n",
"from sklearn.manifold import TSNE\n",
"from zenml.client import Client\n",
"\n",
"artifact = Client().get_artifact_version(\n",
Expand All @@ -32,8 +32,8 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from matplotlib.colors import ListedColormap\n",
"\n",
"embeddings = np.array([doc.embedding for doc in documents])\n",
Expand Down
2 changes: 1 addition & 1 deletion llm-complete-guide/pipelines/llm_basic_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from steps.url_scraper import url_scraper
from steps.web_url_loader import web_url_loader
from zenml import pipeline, Model
from zenml import pipeline


@pipeline
Expand Down
2 changes: 1 addition & 1 deletion llm-complete-guide/pipelines/rag_deployment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from zenml import pipeline
from steps.rag_deployment import gradio_rag_deployment
from zenml import pipeline


@pipeline(enable_cache=False)
Expand Down
6 changes: 2 additions & 4 deletions llm-complete-guide/steps/distilabel_generate_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# or implied. See the License for the specific language governing
# permissions and limitations under the License.

import os
from typing import Annotated, Tuple

from constants import (
Expand All @@ -22,12 +21,11 @@
)
from datasets import Dataset
from distilabel.llms import OpenAILLM
from distilabel.pipeline import Pipeline
from distilabel.steps import LoadDataFromHub
from distilabel.steps.tasks import GenerateSentencePair
from distilabel.pipeline import Pipeline
from zenml import step

from utils.openai_utils import get_openai_api_key
from zenml import step

synthetic_generation_context = """
The text is a chunk from technical documentation of ZenML.
Expand Down
3 changes: 1 addition & 2 deletions llm-complete-guide/steps/eval_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
from pydantic import BaseModel, conint
from structures import TestResult
from utils.llm_utils import process_input_with_retrieval
from zenml import step

from utils.openai_utils import get_openai_api_key
from zenml import step

logging.getLogger().setLevel(logging.WARNING)

Expand Down
3 changes: 1 addition & 2 deletions llm-complete-guide/steps/generate_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
from litellm.exceptions import APIConnectionError, Timeout
from rich import print
from structures import Document
from utils.openai_utils import get_openai_api_key
from zenml import log_artifact_metadata, step
from zenml.logger import get_logger

from utils.openai_utils import get_openai_api_key

logger = get_logger(__name__)
LOCAL_MODEL = "ollama/mixtral"

Expand Down
3 changes: 1 addition & 2 deletions llm-complete-guide/steps/huggingface_dataset_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
# permissions and limitations under the License.

import polars as pl
from constants import SECRET_NAME
from datasets import Dataset
from huggingface_hub import create_repo
from zenml import step
from zenml.client import Client

from constants import SECRET_NAME


@step
def upload_chunks_dataset_to_huggingface(
Expand Down
3 changes: 1 addition & 2 deletions llm-complete-guide/steps/push_to_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
from zenml.client import Client

from constants import DATASET_NAME_DISTILABEL, SECRET_NAME
from datasets import Dataset, DatasetDict
from zenml import step
from zenml.client import Client


@step
Expand Down
29 changes: 20 additions & 9 deletions llm-complete-guide/steps/rag_deployment.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
from zenml import step
import time
import gradio as gr

import gradio as gr
from utils.llm_utils import process_input_with_retrieval
from zenml import step

def slow_echo(message, history):
for i in range(len(message)):
time.sleep(0.05)
yield "You typed: " + message[: i + 1]

def predict(message, history):
return process_input_with_retrieval(
input=message,
n_items_retrieved=20,
use_reranking=True,
)


@step
def gradio_rag_deployment():
demo = gr.ChatInterface(slow_echo, type="messages")
demo.launch()
def gradio_rag_deployment() -> None:
"""Launches a Gradio chat interface with the slow echo demo.
Starts a web server with a chat interface that echoes back user messages.
The server runs indefinitely until manually stopped.
"""
demo = gr.ChatInterface(predict, type="messages")
demo.launch(share=True, inbrowser=True)
# Keep the step running
while True:
time.sleep(1)
6 changes: 2 additions & 4 deletions llm-complete-guide/steps/synthetic_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
from typing import Annotated

import pandas as pd
from constants import SECRET_NAME
from datasets import Dataset
from huggingface_hub import create_repo
from litellm import completion

from constants import SECRET_NAME
from structures import Document
from utils.openai_utils import get_openai_api_key
from zenml import ArtifactConfig, step
from zenml.client import Client

from utils.openai_utils import get_openai_api_key

logger = logging.getLogger(__name__)

LOCAL_MODEL = "ollama/mixtral"
Expand Down
2 changes: 0 additions & 2 deletions llm-complete-guide/utils/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import logging

from zenml.cli import secret
from zenml.client import Client

from utils.openai_utils import get_openai_api_key
Expand All @@ -35,7 +34,6 @@
# Configure the logging level for the root logger
logging.getLogger().setLevel(logging.ERROR)

import os
import re
from typing import List, Tuple

Expand Down
5 changes: 1 addition & 4 deletions llm-complete-guide/utils/openai_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import os

from zenml.client import Client

from constants import SECRET_NAME
from zenml.client import Client


def get_openai_api_key():
Expand Down

0 comments on commit e82b37b

Please sign in to comment.