Skip to content

Commit

Permalink
fix: check hostname validity, throw exception if not correct (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
viveknair committed Apr 25, 2023
1 parent 684b041 commit 6429350
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions package/gentrace/providers/getters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import re
from urllib.parse import urlparse

import openai

Expand All @@ -7,6 +9,9 @@
openai.api_key = os.getenv("OPENAI_KEY")


VALID_GENTRACE_HOST = r"^https?://[\w.-]+:\d{1,5}/api/v1/?$"


def configure_openai():
from gentrace import api_key, host

Expand All @@ -15,6 +20,9 @@ def configure_openai():
if not api_key:
raise ValueError("Gentrace API key not set")

if host and not re.match(VALID_GENTRACE_HOST, host):
raise ValueError("Gentrace host is invalid")

gentrace_config = GentraceConfiguration(host=host)
gentrace_config.access_token = api_key

Expand Down
4 changes: 3 additions & 1 deletion package/tests/test_openai_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ def test_openai_embedding_self_contained_pipeline_id_server(
result = openai.Embedding.create(
input="sample text",
model="text-similarity-davinci-001",
pipeline_id="testing-value",
pipeline_id="testing-value-vivek",
)

assert uuid.UUID(result.pipeline_run_id) is not None

print("pipeline_id: ", result.pipeline_run_id)
print(setup_teardown_openai)


Expand Down
16 changes: 15 additions & 1 deletion package/tests/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
from unittest.mock import create_autospec

import pytest
from urllib3.response import HTTPResponse

import gentrace


def test_gentrace_host_valid():
gentrace.api_key = os.getenv("GENTRACE_API_KEY")
gentrace.host = "http://localhost:3000/"

with pytest.raises(ValueError):
gentrace.configure_openai()

gentrace.host = "http://localhost:3000/api/v1/feedback"
with pytest.raises(ValueError):
gentrace.configure_openai()

gentrace.host = ""
gentrace.api_key = ""


def test_openai_configure_should_raise_error():
with pytest.raises(ValueError):
gentrace.configure_openai()
Expand Down

0 comments on commit 6429350

Please sign in to comment.