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

fix-logged-in-with #1345

Merged
merged 1 commit into from
Feb 5, 2025
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
47 changes: 37 additions & 10 deletions deepeval/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import webbrowser
import typer
from typing import Optional
from rich import print
from deepeval.key_handler import KEY_FILE_HANDLER, KeyValues
from deepeval.cli.test import app as test_app
from deepeval.cli.recommend import app as recommend_app
from deepeval.telemetry import capture_login_event
import webbrowser
import threading
import random
import string
import socket
import typer

from deepeval.key_handler import KEY_FILE_HANDLER, KeyValues
from deepeval.cli.recommend import app as recommend_app
from deepeval.telemetry import capture_login_event, get_logged_in_with
from deepeval.cli.test import app as test_app
from deepeval.cli.server import start_server


PROD = "https://app.confident-ai.com"
LOCAL = "http://localhost:3000"

Expand All @@ -22,6 +26,10 @@ def generate_pairing_code():
"""Generate a random pairing code."""
return "".join(random.choices(string.ascii_uppercase + string.digits, k=6))

def find_available_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("localhost", 0)) # Bind to port 0 to get an available port
return s.getsockname()[1]

@app.command()
def login(
Expand Down Expand Up @@ -57,12 +65,23 @@ def login(
else:
"""Login to the DeepEval platform."""
print("Welcome to :sparkles:[bold]DeepEval[/bold]:sparkles:!")
print(
"Login and grab your API key here: [link=https://app.confident-ai.com]https://app.confident-ai.com[/link] "

# Start the pairing server
port = find_available_port()
pairing_code = generate_pairing_code()
pairing_thread = threading.Thread(
target=start_server, args=(pairing_code, port, PROD), daemon=True
)
webbrowser.open(
"https://app.confident-ai.com/auth/signup?utm_source=deepeval"
pairing_thread.start()

# Open web url
login_url = f"{PROD}/pair?code={pairing_code}&port={port}"
print(
f"Login and grab your API key here: [link={login_url}]{login_url}[/link] "
)
webbrowser.open(login_url)


if api_key == "":
while True:
api_key = input("Paste your API Key: ").strip()
Expand All @@ -74,13 +93,21 @@ def login(
)

KEY_FILE_HANDLER.write_key(KeyValues.API_KEY, api_key)
logged_in_with = get_logged_in_with()
if logged_in_with:
print("Logged in with: ", logged_in_with)
span.set_attribute("completed", False)

print(
"\n🎉🥳 Congratulations! You've successfully logged in! :raising_hands: "
)
print(
"You're now using DeepEval with [rgb(106,0,255)]Confident AI[/rgb(106,0,255)]. Follow our quickstart tutorial here: [bold][link=https://docs.confident-ai.com/confident-ai/confident-ai-introduction]https://docs.confident-ai.com/confident-ai/confident-ai-introduction[/link][/bold]"
)
except:
logged_in_with = get_logged_in_with()
if logged_in_with:
print("\nLogged in with: ", logged_in_with)
span.set_attribute("completed", False)


Expand Down
14 changes: 3 additions & 11 deletions deepeval/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from rich import print
import socketserver
import http.server
import webbrowser
import threading
import json

Expand All @@ -11,7 +10,7 @@
LOGGED_IN_WITH = "logged_in_with"


def start_server(pairing_code: str, prod_url: str) -> str:
def start_server(pairing_code: str, port: str, prod_url: str) -> str:

class PairingHandler(http.server.SimpleHTTPRequestHandler):

Expand All @@ -30,7 +29,6 @@ def do_POST(self):
content_length = int(self.headers["Content-Length"])
body = self.rfile.read(content_length)
data: Dict = json.loads(body)

if self.path == f"/{LOGGED_IN_WITH}":
logged_in_with = data.get(LOGGED_IN_WITH)
pairing_code_recieved = data.get("pairing_code")
Expand All @@ -39,21 +37,15 @@ def do_POST(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", prod_url)
self.end_headers()
print(f"Logged in with: {logged_in_with}")
threading.Thread(target=httpd.shutdown, daemon=True).start()
return

self.send_response(400)
self.send_header("Access-Control-Allow-Origin", prod_url)
self.end_headers()
self.wfile.write(b"Invalid pairing code or data")

with socketserver.TCPServer(("localhost", 0), PairingHandler) as httpd:
port = httpd.server_address[1]
login_url = f"{prod_url}/pair?code={pairing_code}&port={port}"
print(
f"Login and grab your API key here: [link={prod_url}]{prod_url}[/link] "
)
webbrowser.open(login_url)
with socketserver.TCPServer(("localhost", port), PairingHandler) as httpd:
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
thread.join()
Loading