Skip to content

Commit

Permalink
json serialization over http
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-dixon committed Nov 17, 2024
1 parent 651fbfd commit 1209cd8
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 122 deletions.
25 changes: 25 additions & 0 deletions examples/future/http_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pydantic import Field
import ell

ell.init(api_url='http://localhost:8081')

@ell.tool()
def get_weather(location: str = Field(description="The full name of a city and country, e.g. San Francisco, CA, USA")):
"""Get the current weather for a given location."""
# Simulated weather API call
return f"The weather in {location} is sunny."

@ell.complex(model="gpt-4o", tools=[get_weather])
def travel_planner(destination: str):
"""Plan a trip based on the destination and current weather."""
return [
ell.system("You are a travel planner. Use the weather tool to provide relevant advice."),
ell.user(f"Plan a trip to {destination}")
]

result = travel_planner("Paris")
print(result.text) # Prints travel advice
if result.tool_calls:
# This is done so that we can pass the tool calls to the language model
tool_results = result.call_tools_and_collect_as_message()
print("Weather info:", (tool_results.text))
6 changes: 3 additions & 3 deletions src/ell/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def init(
default_api_params: Optional[Dict[str, Any]] = None,
default_client: Optional[Any] = None,
autocommit_model: str = "gpt-4o-mini",
api_server_url: Optional[str] = None,
api_url: Optional[str] = None,
serializer: Optional[EllSerializer] = None,
) -> None:
"""
Expand Down Expand Up @@ -191,7 +191,7 @@ def init(
config.verbose = verbose
config.lazy_versioning = lazy_versioning

if not isinstance(store, str):
if store and not isinstance(store, str):
try:
from ell.serialize.sql import SQLSerializer
config.serializer = SQLSerializer(store)
Expand All @@ -206,7 +206,7 @@ def init(
config.serializer = serializer
else:
serialize_config = SerializeConfig(
api_server_url=api_server_url,
api_url=api_url,
storage_dir=store,
# ...other options
log_level=20 if verbose else 0,
Expand Down
18 changes: 16 additions & 2 deletions src/ell/lmp/_track.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import logging
import threading

from ell.types import Message, ContentBlock, ToolResult
from ell.types.lmp import LMPType
from ell.types.serialize import Invocation, InvocationContents, WriteInvocationInput, utc_now, WriteLMPInput
from ell.util._warnings import _autocommit_warning
Expand Down Expand Up @@ -201,11 +203,22 @@ def _serialize_lmp(func):

def _write_invocation(func, invocation_id, latency_ms, prompt_tokens, completion_tokens,
state_cache_key, invocation_api_params, cleaned_invocation_params, consumes, result, parent_invocation_id):


# print(result)
# todo(alex). figure out what's going on here, looks like we're getting result as a tool result / single message sometimes

results = None
if isinstance(result, list):
results = result
elif isinstance(result, ToolResult):
results = [Message(role='tool', content=[ContentBlock(tool_result=result)])]
else:
results = [result]

invocation_contents = InvocationContents(
invocation_id=invocation_id,
params=cleaned_invocation_params,
results=result,
results=results,
invocation_api_params=invocation_api_params,
global_vars=get_immutable_vars(func.__ell_closure__[2]),
free_vars=get_immutable_vars(func.__ell_closure__[3])
Expand All @@ -217,6 +230,7 @@ def _write_invocation(func, invocation_id, latency_ms, prompt_tokens, completion
# Write to the blob store
blob_id = config.serializer.store_blob(
blob_id=invocation_id,
#todo(alex): normalize serialization
blob=json.dumps(invocation_contents.model_dump(
), default=str, ensure_ascii=False).encode('utf-8'),
)
Expand Down
4 changes: 2 additions & 2 deletions src/ell/serialize/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class SerializeConfig(BaseModel):
storage_dir: Optional[str] = Field(default=None, description="Filesystem path used for SQLite and local blob storage")
api_url: Optional[str] = Field(default=None, description="ell API server endpoint")
pg_connection_string: Optional[str] = None
api_server_endpoint: Optional[str] = Field(default=None, description="Ell API server endpoint")
minio_endpoint: Optional[str] = None
minio_access_key: Optional[str] = None
minio_secret_key: Optional[str] = None
Expand All @@ -29,5 +29,5 @@ def model_post_init(self, __context: Any):

@computed_field
def is_enabled(self) -> bool:
return bool(self.api_server_endpoint or self.pg_connection_string or self.storage_dir or self.minio_endpoint)
return bool(self.api_url or self.pg_connection_string or self.storage_dir or self.minio_endpoint)

Loading

0 comments on commit 1209cd8

Please sign in to comment.