Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate url before post #372

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/log10/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from abc import ABC
from enum import Enum
from typing import List, Optional
from urllib.parse import urljoin, urlparse

import requests

Expand Down Expand Up @@ -163,9 +164,18 @@ def chat_request(self, messages: List[Message], hparams: dict = None) -> dict:
raise Exception("Not implemented")

def api_request(self, rel_url: str, method: str, request: dict):
def is_safe_url(url: str) -> bool:
parsed = urlparse(url)
base_domain = urlparse(self.log10_config.url).netloc
return parsed.netloc == base_domain or not parsed.netloc

full_url = urljoin(self.log10_config.url, rel_url.strip())
if not is_safe_url(full_url):
raise ValueError("Invalid URL: " + full_url)

return requests.request(
method,
f"{self.log10_config.url}{rel_url}",
full_url,
headers={
"x-log10-token": self.log10_config.token,
"Content-Type": "application/json",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_magentic_perplexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
litellm.callbacks = [log10_handler]


@pytest.mark.skip("Unstable, will be fixed in a separate PR")
@pytest.mark.chat
def test_prompt(session, openai_compatibility_model):
@prompt("What is 3 - 3?", model=LitellmChatModel(model=openai_compatibility_model))
Expand All @@ -26,6 +27,7 @@ def llm() -> str: ...
_LogAssertion(completion_id=session.last_completion_id(), message_content=output).assert_chat_response()


@pytest.mark.skip("Unstable, will be fixed in a separate PR")
@pytest.mark.chat
@pytest.mark.stream
def test_prompt_stream(session, openai_compatibility_model):
Expand All @@ -36,4 +38,5 @@ def llm() -> StreamedStr: ...
output = ""
for chunk in response:
output += chunk
time.sleep(3)
_LogAssertion(completion_id=session.last_completion_id(), message_content=output).assert_chat_response()
Loading