From df46f848cca129edca0499c26eab07ada4ac55b9 Mon Sep 17 00:00:00 2001 From: Daniel Dias Date: Tue, 17 Sep 2024 19:33:25 -0300 Subject: [PATCH] update configs --- examples/quick-start-llm/Makefile | 20 ++++++ examples/quick-start-llm/README.md | 11 +++- examples/quick-start-llm/app/Dockerfile | 1 + .../app/__pycache__/telemetry.cpython-312.pyc | Bin 1799 -> 1966 bytes .../llm/__pycache__/providers.cpython-312.pyc | Bin 1088 -> 996 bytes .../app/llm/provider_huggingface_bart.py | 57 ------------------ examples/quick-start-llm/app/llm/providers.py | 2 - examples/quick-start-llm/app/requirements.txt | 1 - examples/quick-start-llm/app/streamlit_app.py | 20 +++--- examples/quick-start-llm/app/telemetry.py | 8 +-- examples/quick-start-llm/docker-compose.yaml | 2 - .../observability/otelcollector.config.yaml | 2 - ...rements.macos.txt => requirements.dev.txt} | 7 +-- 13 files changed, 47 insertions(+), 84 deletions(-) create mode 100644 examples/quick-start-llm/Makefile delete mode 100644 examples/quick-start-llm/app/llm/provider_huggingface_bart.py rename examples/quick-start-llm/{requirements.macos.txt => requirements.dev.txt} (76%) diff --git a/examples/quick-start-llm/Makefile b/examples/quick-start-llm/Makefile new file mode 100644 index 0000000000..0616123bfc --- /dev/null +++ b/examples/quick-start-llm/Makefile @@ -0,0 +1,20 @@ +help: Makefile ## show list of commands + @echo "Choose a command to run:" + @echo "" + @awk 'BEGIN {FS = ":.*?## "} /[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort + +build-images: ## build images used by docker compose file + @docker compose build + +start/with-docker: build-images ## build and run app using docker compose + @docker compose up + +start/with-docker/only-observability: ## run observability stack using docker compose in debug mode + @docker compose up otel-collector jaeger + +start/with-local: ## run app using docker compose + @docker compose up -d otel-collector jaeger + @OTEL_SERVICE_NAME=quick-start-llm OTEL_TRACES_EXPORTER=otlp OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317 opentelemetry-instrument streamlit run ./app/streamlit_app.py + +stop: ## stop all running containers + @docker compose down diff --git a/examples/quick-start-llm/README.md b/examples/quick-start-llm/README.md index 3d50e3b71b..8e25ca0aff 100644 --- a/examples/quick-start-llm/README.md +++ b/examples/quick-start-llm/README.md @@ -13,7 +13,10 @@ python -m venv ./_venv source _venv/bin/activate # install requirements -pip install -r requirements.txt +pip install -r app/requirements.txt + +# install OTel auto-instrumentation +opentelemetry-bootstrap -a install # add openai api key echo "OPENAI_API_KEY={your-open-ai-api-key}" >> .env @@ -22,7 +25,11 @@ echo "OPENAI_API_KEY={your-open-ai-api-key}" >> .env ### Run example ```bash -streamlit run ./app/streamlit_app.py +OTEL_SERVICE_NAME=quick-start-llm \ +OTEL_TRACES_EXPORTER=console,otlp \ +OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=localhost:4317 \ +opentelemetry-instrument \ + streamlit run ./app/streamlit_app.py ``` ### References diff --git a/examples/quick-start-llm/app/Dockerfile b/examples/quick-start-llm/app/Dockerfile index 79acbff57b..89b5c2ee9f 100644 --- a/examples/quick-start-llm/app/Dockerfile +++ b/examples/quick-start-llm/app/Dockerfile @@ -4,5 +4,6 @@ WORKDIR /opt/app COPY . . RUN pip install --no-cache-dir -r requirements.txt +RUN opentelemetry-bootstrap -a install` CMD [ "streamlit", "run", "streamlit_app.py", "--server.port=8051", "--server.address=0.0.0.0" ] diff --git a/examples/quick-start-llm/app/__pycache__/telemetry.cpython-312.pyc b/examples/quick-start-llm/app/__pycache__/telemetry.cpython-312.pyc index 1aca985348ad84a1a7ed369274971c1a7dfce248..51284c467c98bbdab304493baa7acfc04592b95b 100644 GIT binary patch delta 593 zcmZqYTgT6PnwOW00SJ2kyiA)rkynz*j&Y(!U_C<$dk$|DFB3y5Zm{a*v_}1{l z41g;TP8G`1M0E~`mnsZX!N>sA&6uTyMU4fTefOuCwmjHiHzmO`voc!d(oQ(Y95-Ss9 zL-SjLV8y|%L1CWGuJL}3zOIw!Gd^bIo7~KFo=X&HcoB$@nHC7MZ7VzLSAIf+|L`Ng-`(o;)P^U8|EKuSbF1n*=%Hm7>9nIL^D z8Hyx9!XSfwaoFVMr$T2dCPS$3XE7AsXL7`LJ3M4)-Gcq#XWstwi t5Oklx?k2Zk>nqrhhcW=2D>i2#NhNWcI9 diff --git a/examples/quick-start-llm/app/llm/__pycache__/providers.cpython-312.pyc b/examples/quick-start-llm/app/llm/__pycache__/providers.cpython-312.pyc index 872595cc3d0163ac29ea32eac1dfb975281a4ebb..39a289ece08de778281a7c8e9444b72a89c7dee6 100644 GIT binary patch delta 342 zcmX@W@q}IdG%qg~0}zzIf0?#~k%8echyw%6P{wDMiR!Ww10+P!nW9)ym|Ga4SW{Sl zBwHn`Cfmdgg^h<}85y}ID>9kc@&L6IflR!`o1R({Ur>}^mYI@TR9qwo6ypTqVo@N` zz;K6?w5) z*usNsOOXgrhG()hvne~s9e$eZlgpXqCU0ey%wz%bA!dRc0#;Zg0Aj%%{ENdTH$SB` zC)KV<94G+{3x;AXAn}2jk&*E(gT-A2tB-69%;J}sByKVRNy!ha%zR7@T%WiY8ErnZ Ku`u$1%>n=)zDjxk delta 467 zcmaFDet<*$G%qg~0}#ZuzDQ$WVqka-;=lk4l<_%cqPnb03PTEW4ofa;6e}Z8Wh&!p zs4S4pjKpSzvD2BO*iu+p7^2uySb-!*C7UMu!~z8YagWmU^vt|;x5VUBr^KR?jjb7s zj69RyF`C)(0*x%<1`@Y;(^E_03ySi~GE-8Eii?DRVj%HiQ6SO4aEFt(C;6g~?wYF0 zd{&n^tuHWH7x7L`WJ+`70Lp7J6$t>TB2EY!MBUYKo7+y0ew`&GC7V}wulv| z3gQxw^TEa!34&N~Z~o%2$<0qG%}KQ@k_1XH0&%f1kodsN$jEq?!Qw81-CYK&k8BLg n;+L5uZZb(;W|F?i1Y}EoU}xcDYT)|B!^mj!nT>^!4{Rs^gvM+m diff --git a/examples/quick-start-llm/app/llm/provider_huggingface_bart.py b/examples/quick-start-llm/app/llm/provider_huggingface_bart.py deleted file mode 100644 index 779bb28f67..0000000000 --- a/examples/quick-start-llm/app/llm/provider_huggingface_bart.py +++ /dev/null @@ -1,57 +0,0 @@ - -from langchain_community.docstore.document import Document -from langchain_core.prompts import ChatPromptTemplate -from langchain_text_splitters import CharacterTextSplitter -from langchain.chains.combine_documents import create_stuff_documents_chain - -from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace -from langchain import HuggingFaceHub - -import os - -class HuggingFaceBartProvider: - def provider_name(self): - return "Hugging Face (Bart)" - - def summarize(self, text): - huggingfacehub_api_token = os.getenv("HUGGINGFACE_HUB_API_TOKEN", "") - - if not huggingfacehub_api_token.strip(): - raise ValueError("Please provide the HuggingFace API Token on a .env file.") - - llm = HuggingFaceHub( - repo_id="facebook/bart-large-cnn", - task="summarization", - model_kwargs={ - "temperature":0, - "max_length":180, - 'max_new_tokens' : 120, - 'top_k' : 10, - 'top_p': 0.95, - 'repetition_penalty':1.03 - }, - huggingfacehub_api_token=huggingfacehub_api_token - ) - - chat = ChatHuggingFace(llm=llm, verbose=True) - - # Define prompt - prompt = ChatPromptTemplate.from_messages( - [ - ("system", "Write a concise summary of the following:\\n\\n{context}"), - ("human", "") - ] - ) - - # Instantiate chain - chain = create_stuff_documents_chain(chat, prompt) - - # Split the source text - text_splitter = CharacterTextSplitter() - texts = text_splitter.split_text(text) - - # Create Document objects for the texts (max 3 pages) - docs = [Document(page_content=t) for t in texts[:3]] - - # Invoke chain - return chain.invoke({"context": docs}) diff --git a/examples/quick-start-llm/app/llm/providers.py b/examples/quick-start-llm/app/llm/providers.py index 47eaedd480..baa3486100 100644 --- a/examples/quick-start-llm/app/llm/providers.py +++ b/examples/quick-start-llm/app/llm/providers.py @@ -1,10 +1,8 @@ from .provider_google_gemini import GoogleGeminiProvider -from .provider_huggingface_bart import HuggingFaceBartProvider from .provider_openai_chatgpt import OpenAIChatGPTProvider _providers = [ GoogleGeminiProvider(), - HuggingFaceBartProvider(), OpenAIChatGPTProvider() ] diff --git a/examples/quick-start-llm/app/requirements.txt b/examples/quick-start-llm/app/requirements.txt index 07774bbb4f..236ed05703 100644 --- a/examples/quick-start-llm/app/requirements.txt +++ b/examples/quick-start-llm/app/requirements.txt @@ -1,7 +1,6 @@ streamlit==1.38.0 langchain==0.2.16 langchain-openai==0.1.24 -langchain-huggingface==0.0.3 langchain-google-genai==1.0.10 langchain-community==0.2.16 python-dotenv==1.0.1 diff --git a/examples/quick-start-llm/app/streamlit_app.py b/examples/quick-start-llm/app/streamlit_app.py index 30339cbf3f..09180c3b71 100644 --- a/examples/quick-start-llm/app/streamlit_app.py +++ b/examples/quick-start-llm/app/streamlit_app.py @@ -3,10 +3,12 @@ # Load environment variables from .env file load_dotenv() -# Initialize OpenLit -from telemetry import init as telemetry_init, heartbeat as telemetry_heartbeat, otlp_endpoint +# Initialize telemetry +from telemetry import init as telemetry_init, otlp_endpoint tracer = telemetry_init() # run telemetry.init() before loading any other modules to capture any module-level telemetry -telemetry_heartbeat(tracer) + +# from telemetry import heartbeat as telemetry_heartbeat +# telemetry_heartbeat(tracer) import streamlit as st @@ -16,8 +18,13 @@ def read_default_text(): with open("./example.txt") as f: return f.read() +@tracer.start_as_current_span("perform_summarization") +def perform_summarization(provider_type, source_text): + provider = get_provider(provider_type) + return provider.summarize(source_text) + ############################ -# App start +# UI App start ############################ # Streamlit app @@ -37,7 +44,7 @@ def read_default_text(): def callback(): st.session_state['source_text'] = read_default_text() -st.button("Add default text", on_click=callback) +st.button("Add example text", on_click=callback) source_text = st.text_area("Source Text", label_visibility="collapsed", height=250, key="source_text") # If the 'Summarize' button is clicked @@ -48,8 +55,7 @@ def callback(): else: try: with st.spinner('Please wait...'): - provider = get_provider(provider_type) - summary = provider.summarize(source_text) + summary = perform_summarization(provider_type, source_text) st.success(summary) except Exception as e: st.exception(f"An error occurred: {e}") diff --git a/examples/quick-start-llm/app/telemetry.py b/examples/quick-start-llm/app/telemetry.py index 0a426d7a0a..e8bc173ddb 100644 --- a/examples/quick-start-llm/app/telemetry.py +++ b/examples/quick-start-llm/app/telemetry.py @@ -31,16 +31,10 @@ def init(): Traceloop.init( exporter=OTLPSpanExporter(endpoint=otlp_endpoint, insecure=True) ) - # openlit.init( - # environment='development', - # application_name=otlp_service_name, - # tracer=tracer, - # disable_metrics=True, - # collect_gpu_stats=False - # ) return tracer +# Test method to guarantee that the telemetry is working def heartbeat(tracer): with tracer.start_as_current_span("heartbeat"): current_span = trace.get_current_span() diff --git a/examples/quick-start-llm/docker-compose.yaml b/examples/quick-start-llm/docker-compose.yaml index 2e4041860c..f60474c79f 100644 --- a/examples/quick-start-llm/docker-compose.yaml +++ b/examples/quick-start-llm/docker-compose.yaml @@ -6,7 +6,6 @@ services: environment: - OTEL_SERVICE_NAME=quick-start-llm - OTEL_EXPORTER_OTLP_ENDPOINT=otel-collector:4317 - - HUGGINGFACE_HUB_API_TOKEN=${HUGGINGFACE_HUB_API_TOKEN} - GOOGLE_API_KEY=${GOOGLE_API_KEY} - OPENAI_API_KEY=${OPENAI_API_KEY} ports: @@ -24,7 +23,6 @@ services: image: otel/opentelemetry-collector:0.108.0 ports: - 4317:4317 - - 4318:4318 volumes: - type: bind source: ./observability/otelcollector.config.yaml diff --git a/examples/quick-start-llm/observability/otelcollector.config.yaml b/examples/quick-start-llm/observability/otelcollector.config.yaml index b1ca50439e..f531c42da0 100644 --- a/examples/quick-start-llm/observability/otelcollector.config.yaml +++ b/examples/quick-start-llm/observability/otelcollector.config.yaml @@ -3,8 +3,6 @@ receivers: protocols: grpc: endpoint: 0.0.0.0:4317 - http: - endpoint: 0.0.0.0:4318 processors: batch: diff --git a/examples/quick-start-llm/requirements.macos.txt b/examples/quick-start-llm/requirements.dev.txt similarity index 76% rename from examples/quick-start-llm/requirements.macos.txt rename to examples/quick-start-llm/requirements.dev.txt index 4be57dc19f..0477d0cd6e 100644 --- a/examples/quick-start-llm/requirements.macos.txt +++ b/examples/quick-start-llm/requirements.dev.txt @@ -1,12 +1,11 @@ streamlit==1.38.0 langchain==0.2.16 langchain-openai==0.1.24 -langchain-huggingface==0.0.3 langchain-google-genai==1.0.10 langchain-community==0.2.16 python-dotenv==1.0.1 -openlit==1.22.2 -watchdog==5.0.2 -opentelemetry-distro==0.45b0 +opentelemetry-distro==0.48b0 opentelemetry-exporter-otlp==1.27.0 opentelemetry-exporter-otlp-proto-grpc==1.27.0 +traceloop-sdk==0.30.0 +watchdog==5.0.2