Skip to content

Commit

Permalink
Merge pull request #24 from confident-ai/main
Browse files Browse the repository at this point in the history
Merge from main.
  • Loading branch information
Anindyadeep authored Dec 29, 2023
2 parents b7174e8 + 5e3f12a commit 17cf20a
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 77 deletions.
80 changes: 79 additions & 1 deletion deepeval/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import requests
import warnings
from requests.adapters import HTTPAdapter, Response, Retry
import aiohttp
from aiohttp import ClientSession
from requests.adapters import HTTPAdapter
from enum import Enum

from deepeval.constants import API_KEY_ENV
from deepeval.key_handler import KEY_FILE_HANDLER, KeyValues
from enum import Enum

API_BASE_URL = "https://app.confident-ai.com/api"

Expand Down Expand Up @@ -259,3 +263,77 @@ def quote_string(text: str) -> str:
str: Quoted text in return
"""
return urllib.parse.quote(text, safe="")

async def _api_request_async(
self,
method,
endpoint,
headers=None,
auth=None,
params=None,
body=None,
files=None,
data=None,
):
"""Generic asynchronous HTTP request method with error handling."""
url = f"{self.base_api_url}/{endpoint}"
async with ClientSession() as session:
try:
# Preparing the request body for file uploads if files are present
if files:
data = aiohttp.FormData()
for file_name, file_content in files.items():
data.add_field(
file_name, file_content, filename=file_name
)

# Sending the request
res = await session.request(
method=method,
url=url,
headers=headers,
params=params,
json=body,
)

# Check response status
if res.status == 200:
try:
json = await res.json()
return json
except ValueError:
return await res.text()
else:
# Determine how to process the response based on Content-Type
content_type = res.headers.get("Content-Type", "")
if "application/json" in content_type:
error_response = await res.json()
else:
error_response = await res.text()

# Specifically handle status code 400
if res.status == 400:
print(f"Error 400: Bad Request - {error_response}")

print(f"Error {res.status}: {error_response}")
return None

except Exception as err:
raise Exception(f"HTTP request failed: {err}") from err

async def post_request_async(
self, endpoint, body=None, files=None, data=None
):
"""Generic asynchronous POST Request Wrapper"""
print("hi")
return await self._api_request_async(
"POST",
endpoint,
headers=self._headers
if files is None
else self._headers_multipart_form_data,
auth=self._auth,
body=body,
files=files,
data=data,
)
26 changes: 19 additions & 7 deletions deepeval/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional, List, Dict
from deepeval.api import Api, Endpoints
import threading
import asyncio
from pydantic import BaseModel, Field


Expand Down Expand Up @@ -32,7 +34,18 @@ def track(
conversation_id: Optional[str] = None,
additional_data: Optional[Dict] = None,
fail_silently: Optional[bool] = True,
run_on_background_thread: Optional[bool] = True,
):
def track_event(event: APIEvent, api: Api, fail_silently: bool):
try:
_ = api.post_request(
endpoint=Endpoints.EVENT_ENDPOINT.value,
body=event.dict(by_alias=True, exclude_none=True),
)
except Exception as e:
if not fail_silently:
raise e

event = APIEvent(
name=event_name,
model=model,
Expand All @@ -47,11 +60,10 @@ def track(
additionalData=additional_data,
)
api = Api()
try:
_ = api.post_request(
endpoint=Endpoints.EVENT_ENDPOINT.value,
body=event.dict(by_alias=True, exclude_none=True),
if run_on_background_thread:
thread = threading.Thread(
target=track_event, args=(event, api, fail_silently), daemon=True
)
except Exception as e:
if not fail_silently:
raise (e)
thread.start()
else:
track_event(event, api, fail_silently)
2 changes: 1 addition & 1 deletion docs/docs/confident-ai-introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Continuous evaluation refers to the process of evaluating LLM applications in no
/>
</div>

Everything in `deepeval` is already automatically integrated with Confident AI, including `deepeval`'s [custom metrics](evaluation-metrics#custom-metrics). To start using Confident AI with `deepeval`, simply login in the CLI:
Everything in `deepeval` is already automatically integrated with Confident AI, including `deepeval`'s [custom metrics](metrics-custom). To start using Confident AI with `deepeval`, simply login in the CLI:

```
deepeval login
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/evaluation-test-cases.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test_case = LLMTestCase(

An expected output is literally what you would want the ideal output to be. Note that this parameter is **optional** depending on the metric you want to evaluate.

The expected output doesn't have to exactly match the actual output in order for your test case to pass since `deepeval` uses a variety of methods to evaluate non-deterministic LLM outputs. We'll go into more details [in the metrics section.](evaluation-metrics)
The expected output doesn't have to exactly match the actual output in order for your test case to pass since `deepeval` uses a variety of methods to evaluate non-deterministic LLM outputs. We'll go into more details [in the metrics section.](metrics-introduction)

```python
# A hypothetical LLM application example
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ In your newly created virtual environement, run:
pip install -U deepeval
```

You can also keep track of all evaluation results by logging into our [in all one evaluation platform](https://confident-ai.com), and use Confident AI's [proprietary LLM evaluation agent](evaluation-metrics#judgementalgpt) for evaluation:
You can also keep track of all evaluation results by logging into our [in all one evaluation platform](https://confident-ai.com), and use Confident AI's [proprietary LLM evaluation agent](metrics-judgemental) for evaluation:

```console
deepeval login
Expand Down
Loading

0 comments on commit 17cf20a

Please sign in to comment.