Skip to content

Commit

Permalink
git output via openai migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeldking committed Nov 7, 2023
1 parent 172307d commit 75ba27d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
4 changes: 1 addition & 3 deletions scripts/rag/llama_index_w_evals_and_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
)
Expand Down
6 changes: 4 additions & 2 deletions src/phoenix/experimental/evals/retrievals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
30 changes: 17 additions & 13 deletions tests/trace/openai/test_instrumentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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())

Expand Down Expand Up @@ -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]
Expand Down
3 changes: 0 additions & 3 deletions tutorials/build_arize_docs_index_langchain_pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/"
Expand Down

0 comments on commit 75ba27d

Please sign in to comment.