From 75ba27ddb811b15134566dd09e2fb88cc1fc337a Mon Sep 17 00:00:00 2001 From: Mikyo King Date: Tue, 7 Nov 2023 08:31:43 -0700 Subject: [PATCH] git output via openai migrate --- scripts/rag/llama_index_w_evals_and_qa.py | 4 +-- src/phoenix/experimental/evals/retrievals.py | 6 ++-- tests/trace/openai/test_instrumentor.py | 30 +++++++++++-------- ...ild_arize_docs_index_langchain_pinecone.py | 3 -- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/scripts/rag/llama_index_w_evals_and_qa.py b/scripts/rag/llama_index_w_evals_and_qa.py index ba9d56ca7a..7d791c2782 100644 --- a/scripts/rag/llama_index_w_evals_and_qa.py +++ b/scripts/rag/llama_index_w_evals_and_qa.py @@ -11,7 +11,6 @@ import cohere import numpy as np -import openai import pandas as pd import phoenix.experimental.evals.templates.default_templates as templates import requests @@ -380,8 +379,7 @@ def process_row(row, formatted_evals_column, k): def check_keys() -> None: - openai.api_key = os.getenv("OPENAI_API_KEY") - if openai.api_key is None: + if os.getenv("OPENAI_API_KEY") is None: raise RuntimeError( "OpenAI API key missing. Please set it up in your environment as OPENAI_API_KEY" ) diff --git a/src/phoenix/experimental/evals/retrievals.py b/src/phoenix/experimental/evals/retrievals.py index 671c139538..127cca0973 100644 --- a/src/phoenix/experimental/evals/retrievals.py +++ b/src/phoenix/experimental/evals/retrievals.py @@ -75,13 +75,15 @@ def classify_relevance(query: str, document: str, model_name: str) -> Optional[b unparseable output. """ - from openai import ChatCompletion + from openai import OpenAI + + client = OpenAI() prompt = _QUERY_CONTEXT_PROMPT_TEMPLATE.format( query=query, reference=document, ) - response = ChatCompletion.create( # type: ignore + response = client.chat.completions.create( messages=[ {"role": "system", "content": _EVALUATION_SYSTEM_MESSAGE}, {"role": "user", "content": prompt}, diff --git a/tests/trace/openai/test_instrumentor.py b/tests/trace/openai/test_instrumentor.py index e8e8020bf7..f0dc271fc2 100644 --- a/tests/trace/openai/test_instrumentor.py +++ b/tests/trace/openai/test_instrumentor.py @@ -4,6 +4,7 @@ import openai import pytest import responses +from openai import OpenAI from openai.error import AuthenticationError from phoenix.trace.openai.instrumentor import OpenAIInstrumentor from phoenix.trace.schemas import SpanException, SpanKind, SpanStatusCode @@ -41,14 +42,18 @@ def reload_openai_api_requestor() -> None: @pytest.fixture def openai_api_key(monkeypatch) -> None: api_key = "sk-0123456789" - openai.api_key = api_key monkeypatch.setenv("OPENAI_API_KEY", api_key) return api_key +@pytest.fixture +def client(openai_api_key) -> OpenAI: + return OpenAI(api_key=openai_api_key) + + @responses.activate def test_openai_instrumentor_includes_llm_attributes_on_chat_completion_success( - reload_openai_api_requestor, openai_api_key + reload_openai_api_requestor, client ) -> None: tracer = Tracer() OpenAIInstrumentor(tracer).instrument() @@ -77,7 +82,9 @@ def test_openai_instrumentor_includes_llm_attributes_on_chat_completion_success( }, status=200, ) - response = openai.ChatCompletion.create(model=model, messages=messages, temperature=temperature) + response = client.chat.completions.create( + model=model, messages=messages, temperature=temperature + ) response_text = response.choices[0]["message"]["content"] assert response_text == expected_response_text @@ -166,7 +173,7 @@ def test_openai_instrumentor_includes_function_call_attributes( }, status=200, ) - response = openai.ChatCompletion.create(model=model, messages=messages, functions=functions) + response = client.chat.completions.create(model=model, messages=messages, functions=functions) function_call_data = response.choices[0]["message"]["function_call"] assert set(function_call_data.keys()) == {"name", "arguments"} @@ -206,7 +213,7 @@ def test_openai_instrumentor_includes_function_call_attributes( @responses.activate def test_openai_instrumentor_includes_function_call_message_attributes( - reload_openai_api_requestor, openai_api_key + reload_openai_api_requestor, client ) -> None: tracer = Tracer() OpenAIInstrumentor(tracer).instrument() @@ -269,7 +276,7 @@ def test_openai_instrumentor_includes_function_call_message_attributes( status=200, ) - response = openai.ChatCompletion.create(model=model, messages=messages, functions=functions) + response = client.chat.completions.create(model=model, messages=messages, functions=functions) response_text = response.choices[0]["message"]["content"] spans = list(tracer.get_spans()) span = spans[0] @@ -326,7 +333,7 @@ def test_openai_instrumentor_records_authentication_error( messages = [{"role": "user", "content": "Who won the World Cup in 2018?"}] with pytest.raises(AuthenticationError): - openai.ChatCompletion.create(model=model, messages=messages) + client.chat.completions.create(model=model, messages=messages) spans = list(tracer.get_spans()) assert len(spans) == 1 @@ -343,7 +350,7 @@ def test_openai_instrumentor_records_authentication_error( @responses.activate def test_openai_instrumentor_does_not_interfere_with_completions_api( - reload_openai_api_requestor, openai_api_key + reload_openai_api_requestor, client ) -> None: tracer = Tracer() OpenAIInstrumentor(tracer).instrument() @@ -368,10 +375,7 @@ def test_openai_instrumentor_does_not_interfere_with_completions_api( }, status=200, ) - response = openai.Completion.create( - model=model, - prompt=prompt, - ) + response = client.completions.create(model=model, prompt=prompt) response_text = response.choices[0]["text"] spans = list(tracer.get_spans()) @@ -409,7 +413,7 @@ def test_openai_instrumentor_instrument_method_is_idempotent( }, status=200, ) - response = openai.ChatCompletion.create(model=model, messages=messages) + response = client.chat.completions.create(model=model, messages=messages) response_text = response.choices[0]["message"]["content"] spans = list(tracer.get_spans()) span = spans[0] diff --git a/tutorials/build_arize_docs_index_langchain_pinecone.py b/tutorials/build_arize_docs_index_langchain_pinecone.py index 15c56a2eb2..f521843f4d 100644 --- a/tutorials/build_arize_docs_index_langchain_pinecone.py +++ b/tutorials/build_arize_docs_index_langchain_pinecone.py @@ -15,7 +15,6 @@ from typing import Dict, List, Optional import numpy as np -import openai import pandas as pd import pinecone # type: ignore import tiktoken @@ -147,10 +146,8 @@ def _convert_text_to_embedding_map_to_dataframe( pinecone_api_key = args.pinecone_api_key pinecone_index_name = args.pinecone_index_name pinecone_environment = args.pinecone_environment - openai_api_key = args.openai_api_key output_parquet_path = args.output_parquet_path - openai.api_key = openai_api_key pinecone.init(api_key=pinecone_api_key, environment=pinecone_environment) docs_url = "https://docs.arize.com/arize/"