From 993f631b854c6621f2210c6bcb405fdc218b7be8 Mon Sep 17 00:00:00 2001 From: Daniel Dias Date: Mon, 30 Sep 2024 16:04:55 -0300 Subject: [PATCH] fix(llm-example): updated test files --- examples/quick-start-llm-python/README.md | 2 +- .../app/llm/provider_google_gemini.py | 6 ++ .../app/llm/provider_openai_chatgpt.py | 5 +- .../app/llm/providers.py | 8 ++- .../docker-compose.yaml | 4 +- .../e2e/{api.spec.js => chatgpt.api.spec.js} | 20 ------ .../tests/e2e/gemini.api.spec.js | 25 +++++++ .../tests/e2e/ui.spec.js | 67 ------------------- .../quick-start-llm-python/tests/package.json | 3 +- 9 files changed, 45 insertions(+), 95 deletions(-) rename examples/quick-start-llm-python/tests/e2e/{api.spec.js => chatgpt.api.spec.js} (51%) create mode 100644 examples/quick-start-llm-python/tests/e2e/gemini.api.spec.js delete mode 100644 examples/quick-start-llm-python/tests/e2e/ui.spec.js diff --git a/examples/quick-start-llm-python/README.md b/examples/quick-start-llm-python/README.md index 4a71b50342..98964a859e 100644 --- a/examples/quick-start-llm-python/README.md +++ b/examples/quick-start-llm-python/README.md @@ -28,7 +28,7 @@ opentelemetry-bootstrap -a install # add openai api key echo "OPENAI_API_KEY={your-open-ai-api-key}" >> .env -# add google gemini api key +# add google gemini api key (optional) echo "GOOGLE_API_KEY={your-google-gemini-api-key}" >> .env # add tracetest agent keys diff --git a/examples/quick-start-llm-python/app/llm/provider_google_gemini.py b/examples/quick-start-llm-python/app/llm/provider_google_gemini.py index 6d1c24e2ed..3521b12a84 100644 --- a/examples/quick-start-llm-python/app/llm/provider_google_gemini.py +++ b/examples/quick-start-llm-python/app/llm/provider_google_gemini.py @@ -4,10 +4,16 @@ from langchain_google_genai import ChatGoogleGenerativeAI +import os + class GoogleGeminiProvider: def provider_name(self): return "Google (Gemini)" + def enabled(self): + gemini_api_key = os.getenv("GOOGLE_API_KEY", "") + return gemini_api_key != "" + def summarize(self, text): chat = ChatGoogleGenerativeAI(model="gemini-pro") diff --git a/examples/quick-start-llm-python/app/llm/provider_openai_chatgpt.py b/examples/quick-start-llm-python/app/llm/provider_openai_chatgpt.py index 9bcef05763..bb2f1b9c0e 100644 --- a/examples/quick-start-llm-python/app/llm/provider_openai_chatgpt.py +++ b/examples/quick-start-llm-python/app/llm/provider_openai_chatgpt.py @@ -5,13 +5,16 @@ from langchain_openai import ChatOpenAI -import streamlit as st import os class OpenAIChatGPTProvider: def provider_name(self): return "OpenAI (ChatGPT)" + def enabled(self): + openai_api_key = os.getenv("OPENAI_API_KEY", "") + return openai_api_key != "" + def summarize(self, text): # Get OpenAI API key and URL to be summarized openai_api_key = os.getenv("OPENAI_API_KEY", "") diff --git a/examples/quick-start-llm-python/app/llm/providers.py b/examples/quick-start-llm-python/app/llm/providers.py index baa3486100..200439eed4 100644 --- a/examples/quick-start-llm-python/app/llm/providers.py +++ b/examples/quick-start-llm-python/app/llm/providers.py @@ -7,7 +7,13 @@ ] def get_providers(): - return list(map(lambda p: p.provider_name(), _providers)) + providers = [] + + for provider in _providers: + if provider.enabled(): + providers.append(provider.provider_name()) + + return providers def get_provider(provider_name): for provider in _providers: diff --git a/examples/quick-start-llm-python/docker-compose.yaml b/examples/quick-start-llm-python/docker-compose.yaml index 94eaa7c16e..84e9100ea7 100644 --- a/examples/quick-start-llm-python/docker-compose.yaml +++ b/examples/quick-start-llm-python/docker-compose.yaml @@ -53,9 +53,7 @@ services: ports: - 4317:4317 volumes: - - type: bind - source: ./observability/otelcollector.config.yaml - target: /otel-local-config.yaml + - ./observability/otelcollector.config.yaml:/otel-local-config.yaml jaeger: healthcheck: diff --git a/examples/quick-start-llm-python/tests/e2e/api.spec.js b/examples/quick-start-llm-python/tests/e2e/chatgpt.api.spec.js similarity index 51% rename from examples/quick-start-llm-python/tests/e2e/api.spec.js rename to examples/quick-start-llm-python/tests/e2e/chatgpt.api.spec.js index cee797c747..cc2c25306d 100644 --- a/examples/quick-start-llm-python/tests/e2e/api.spec.js +++ b/examples/quick-start-llm-python/tests/e2e/chatgpt.api.spec.js @@ -1,30 +1,10 @@ // @ts-check const { test, expect } = require('@playwright/test'); -const geminiTraceBasedTest = require('./definitions/gemini'); const chatgptTraceBasedTest = require('./definitions/chatgpt'); const { runTracebasedTest } = require('./tracetest'); -test('generated summarized test for Gemini', async ({ request }) => { - const result = await request.post(`http://localhost:8800/summarizeText`, { - data: { - provider: "Google (Gemini)", - text: "Born in London, Turing was raised in southern England. He graduated from King's College, Cambridge, and in 1938, earned a doctorate degree from Princeton University. During World War II, Turing worked for the Government Code and Cypher School at Bletchley Park, Britain's codebreaking centre that produced Ultra intelligence. He led Hut 8, the section responsible for German naval cryptanalysis. Turing devised techniques for speeding the breaking of German ciphers, including improvements to the pre-war Polish bomba method, an electromechanical machine that could find settings for the Enigma machine. He played a crucial role in cracking intercepted messages that enabled the Allies to defeat the Axis powers in many crucial engagements, including the Battle of the Atlantic.\n\nAfter the war, Turing worked at the National Physical Laboratory, where he designed the Automatic Computing Engine, one of the first designs for a stored-program computer. In 1948, Turing joined Max Newman's Computing Machine Laboratory at the Victoria University of Manchester, where he helped develop the Manchester computers[12] and became interested in mathematical biology. Turing wrote on the chemical basis of morphogenesis and predicted oscillating chemical reactions such as the Belousov–Zhabotinsky reaction, first observed in the 1960s. Despite these accomplishments, he was never fully recognised during his lifetime because much of his work was covered by the Official Secrets Act." - } - }); - - const jsonResult = await result.json(); - expect(jsonResult).not.toBe(null); - expect(jsonResult.summary).not.toBe(null); - - const traceID = jsonResult.trace_id; - expect(traceID).not.toBe(null); - - // run trace-based test - await runTracebasedTest(geminiTraceBasedTest, traceID); -}); - test('generated summarized test for OpenAI', async ({ request }) => { const result = await request.post(`http://localhost:8800/summarizeText`, { data: { diff --git a/examples/quick-start-llm-python/tests/e2e/gemini.api.spec.js b/examples/quick-start-llm-python/tests/e2e/gemini.api.spec.js new file mode 100644 index 0000000000..343eec3baa --- /dev/null +++ b/examples/quick-start-llm-python/tests/e2e/gemini.api.spec.js @@ -0,0 +1,25 @@ +// @ts-check +const { test, expect } = require('@playwright/test'); + +const geminiTraceBasedTest = require('./definitions/gemini'); + +const { runTracebasedTest } = require('./tracetest'); + +test('generated summarized test for Gemini', async ({ request }) => { + const result = await request.post(`http://localhost:8800/summarizeText`, { + data: { + provider: "Google (Gemini)", + text: "Born in London, Turing was raised in southern England. He graduated from King's College, Cambridge, and in 1938, earned a doctorate degree from Princeton University. During World War II, Turing worked for the Government Code and Cypher School at Bletchley Park, Britain's codebreaking centre that produced Ultra intelligence. He led Hut 8, the section responsible for German naval cryptanalysis. Turing devised techniques for speeding the breaking of German ciphers, including improvements to the pre-war Polish bomba method, an electromechanical machine that could find settings for the Enigma machine. He played a crucial role in cracking intercepted messages that enabled the Allies to defeat the Axis powers in many crucial engagements, including the Battle of the Atlantic.\n\nAfter the war, Turing worked at the National Physical Laboratory, where he designed the Automatic Computing Engine, one of the first designs for a stored-program computer. In 1948, Turing joined Max Newman's Computing Machine Laboratory at the Victoria University of Manchester, where he helped develop the Manchester computers[12] and became interested in mathematical biology. Turing wrote on the chemical basis of morphogenesis and predicted oscillating chemical reactions such as the Belousov–Zhabotinsky reaction, first observed in the 1960s. Despite these accomplishments, he was never fully recognised during his lifetime because much of his work was covered by the Official Secrets Act." + } + }); + + const jsonResult = await result.json(); + expect(jsonResult).not.toBe(null); + expect(jsonResult.summary).not.toBe(null); + + const traceID = jsonResult.trace_id; + expect(traceID).not.toBe(null); + + // run trace-based test + await runTracebasedTest(geminiTraceBasedTest, traceID); +}); diff --git a/examples/quick-start-llm-python/tests/e2e/ui.spec.js b/examples/quick-start-llm-python/tests/e2e/ui.spec.js deleted file mode 100644 index 5404a07110..0000000000 --- a/examples/quick-start-llm-python/tests/e2e/ui.spec.js +++ /dev/null @@ -1,67 +0,0 @@ -// @ts-check -const { test, expect } = require('@playwright/test'); - -const geminiTraceBasedTest = require('./definitions/gemini'); -const chatgptTraceBasedTest = require('./definitions/chatgpt'); - -const { runTracebasedTest } = require('./tracetest'); - -test('generated summarized test for Gemini', async ({ page }) => { - // Go to Streamlit app - await page.goto('http://localhost:8501/'); - - // Select Google (Gemini) model - await page.getByTestId('stSelectbox').locator('div').filter({ hasText: 'Google (Gemini)' }).nth(2).click(); - - // Click on add example text - await page.getByRole('button', { name: 'Add example text' }).click(); - - // Click on button to call summarization rule - await page.getByRole('button', { name: 'Summarize' }).click(); - - // Wait link to appear - await page.getByText('Trace ID').waitFor({ state: 'visible' }); - - // Capture TraceID - const traceIDElement = await page.getByRole('link'); - console.log(traceIDElement.innerHTML()); - - // const traceIDLabel = await page.getByRole('link', { name: 'Trace ID' }); - // expect(traceIDLabel).toHaveText('Trace ID'); - - // console.log(traceIDLabel); - - // const traceID = (traceIDLabel || '').replace('Trace ID:', '').trim(); - - // // run trace-based test - // await runTracebasedTest(geminiTraceBasedTest, traceID); -}); - -// test('generated summarized test for OpenAPI', async ({ page }) => { -// // Go to Streamlit app -// await page.goto('http://localhost:8501/'); - -// // Select OpenAI (ChatGPT) model -// await page.getByTestId('stSelectbox').locator('div').filter({ hasText: 'OpenAI (ChatGPT)' }).nth(2).click(); - -// // Click on add example text -// await page.getByRole('button', { name: 'Add example text' }).click(); - -// // Click on button to call summarization rule -// await page.getByRole('button', { name: 'Summarize' }).click(); - -// // Wait for time -// await sleep(timeToWait); - -// // Capture TraceID -// const traceIDElement = await page.getByText('Trace ID:'); -// expect(traceIDElement).toHaveText('Trace ID:'); - -// const traceIDLabel = await page.getByText('Trace ID:').innerText(); -// expect(traceIDLabel).not.toBeNull(); - -// const traceID = (traceIDLabel || '').replace('Trace ID:', '').trim(); - -// // run trace-based test -// await runTracebasedTest(chatgptTraceBasedTest, traceID); -// }); diff --git a/examples/quick-start-llm-python/tests/package.json b/examples/quick-start-llm-python/tests/package.json index 9013136c9d..e626e2fa0a 100644 --- a/examples/quick-start-llm-python/tests/package.json +++ b/examples/quick-start-llm-python/tests/package.json @@ -5,8 +5,7 @@ "main": "index.js", "scripts": { "record-test": "npx playwright codegen localhost:8501", - "api-tests": "npx playwright test **/*api.spec.js", - "ui-tests": "npx playwright test **/*ui.spec.js" + "api-tests": "npx playwright test **/*api.spec.js" }, "keywords": [], "author": "",