Skip to content

Commit 98dd0ca

Browse files
committed
Final changes
1 parent d45d5fd commit 98dd0ca

File tree

29 files changed

+200
-164
lines changed

29 files changed

+200
-164
lines changed

agent_apis/src/functions/weather.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import aiohttp
2-
from restack_ai.function import function, log
2+
from restack_ai.function import NonRetryableError, function, log
33

44
HTTP_OK = 200
55

@@ -21,6 +21,6 @@ async def weather() -> str:
2121
return str(data)
2222
error_message = f"Error: {response.status}"
2323
raise_exception(error_message)
24-
except Exception:
25-
log.error("Error: {e}")
26-
raise
24+
except Exception as e:
25+
error_message = f"Error: {e}"
26+
raise NonRetryableError(error_message) from e

agent_apis/src/workflows/multistep.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22

33
from pydantic import BaseModel, Field
4-
from restack_ai.workflow import import_functions, log, workflow, NonRetryableError
4+
from restack_ai.workflow import NonRetryableError, import_functions, log, workflow
55

66
with import_functions():
77
from src.functions.llm import FunctionInputParams, llm

agent_rag/src/agents/chat_rag.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22

33
from pydantic import BaseModel
4-
from restack_ai.agent import agent, import_functions, log, NonRetryableError
4+
from restack_ai.agent import NonRetryableError, agent, import_functions, log
55

66
with import_functions():
77
from src.functions.llm_chat import LlmChatInput, Message, llm_chat

agent_rag/src/functions/lookup_sales.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pydantic import BaseModel
2-
from restack_ai.function import function, log
2+
from restack_ai.function import NonRetryableError, function, log
33

44

55
class SalesItem(BaseModel):
@@ -85,5 +85,5 @@ async def lookup_sales() -> str:
8585

8686
return str(items)
8787
except Exception as e:
88-
log.error("lookup_sales function failed", error=e)
89-
raise
88+
error_message = f"lookup_sales function failed: {e}"
89+
raise NonRetryableError(error_message) from e

agent_stream/src/functions/llm_chat.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from openai import OpenAI
55
from pydantic import BaseModel, Field
6-
from restack_ai.function import function, log, stream_to_websocket
6+
from restack_ai.function import NonRetryableError, function, stream_to_websocket
77

88
from src.client import api_address
99

@@ -49,5 +49,5 @@ async def llm_chat(function_input: LlmChatInput) -> str:
4949
return await stream_to_websocket(api_address=api_address, data=response)
5050

5151
except Exception as e:
52-
log.error("llm_chat function failed", error=str(e))
53-
raise
52+
error_message = f"llm_chat function failed: {e}"
53+
raise NonRetryableError(error_message) from e

agent_telephony/twilio/agent_twilio/src/functions/livekit_dispatch.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from livekit import api
55
from livekit.protocol.agent_dispatch import AgentDispatch
6-
from restack_ai.function import function, function_info, log
6+
from restack_ai.function import NonRetryableError, function, function_info
77

88

99
@dataclass
@@ -37,8 +37,8 @@ async def livekit_dispatch(function_input: LivekitDispatchInput) -> AgentDispatc
3737
await lkapi.aclose()
3838

3939
except Exception as e:
40-
log.error("livekit_dispatch function failed", error=str(e))
41-
raise
40+
error_message = f"livekit_dispatch function failed: {e}"
41+
raise NonRetryableError(error_message) from e
4242

4343
else:
4444
return dispatch

agent_telephony/twilio/agent_twilio/src/functions/livekit_room.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from livekit import api
44
from livekit.api import CreateRoomRequest, Room
5-
from restack_ai.function import function, function_info, log
5+
from restack_ai.function import NonRetryableError, function, function_info
66

77

88
@function.defn()
@@ -27,8 +27,8 @@ async def livekit_room() -> Room:
2727
await lkapi.aclose()
2828

2929
except Exception as e:
30-
log.error("livekit_dispatch function failed", error=str(e))
31-
raise
30+
error_message = f"livekit_room function failed: {e}"
31+
raise NonRetryableError(error_message) from e
3232

3333
else:
3434
return room

agent_telephony/twilio/agent_twilio/src/functions/llm_chat.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from openai import OpenAI
55
from pydantic import BaseModel, Field
6-
from restack_ai.function import function, log, stream_to_websocket
6+
from restack_ai.function import NonRetryableError, function, stream_to_websocket
77

88
from src.client import api_address
99

@@ -48,5 +48,5 @@ async def llm_chat(function_input: LlmChatInput) -> str:
4848
return await stream_to_websocket(api_address=api_address, data=response)
4949

5050
except Exception as e:
51-
log.error("llm_chat function failed", error=str(e))
52-
raise
51+
error_message = f"llm_chat function failed: {e}"
52+
raise NonRetryableError(error_message) from e

agent_telephony/vapi/agent_vapi/src/agents/agent.py

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22

33
from pydantic import BaseModel
4-
from restack_ai.agent import agent, import_functions, log
4+
from restack_ai.agent import NonRetryableError, agent, import_functions, log
55

66
with import_functions():
77
from src.functions.llm_chat import LlmChatInput, Message, llm_chat
@@ -32,27 +32,38 @@ async def messages(self, messages_event: MessagesEvent) -> list[Message]:
3232
log.info(f"Received message: {messages_event.messages}")
3333
self.messages.extend(messages_event.messages)
3434

35-
assistant_message = await agent.step(
36-
function=llm_chat,
37-
function_input=LlmChatInput(messages=self.messages),
38-
start_to_close_timeout=timedelta(seconds=120),
39-
)
40-
self.messages.append(Message(role="assistant", content=str(assistant_message)))
41-
return self.messages
35+
try:
36+
assistant_message = await agent.step(
37+
function=llm_chat,
38+
function_input=LlmChatInput(messages=self.messages),
39+
start_to_close_timeout=timedelta(seconds=120),
40+
)
41+
except Exception as e:
42+
error_message = f"llm_chat function failed: {e}"
43+
raise NonRetryableError(error_message) from e
44+
else:
45+
self.messages.append(Message(role="assistant", content=str(assistant_message)))
46+
return self.messages
4247

4348
@agent.event
4449
async def call(self, call_input: CallInput) -> None:
4550
log.info("Call", call_input=call_input)
4651
assistant_id = call_input.assistant_id
4752
phone_number = call_input.phone_number
4853

49-
return await agent.step(
50-
function=vapi_call,
51-
function_input=VapiCallInput(
52-
assistant_id=assistant_id,
53-
phone_number=phone_number,
54-
),
55-
)
54+
try:
55+
result = agent.step(
56+
function=vapi_call,
57+
function_input=VapiCallInput(
58+
assistant_id=assistant_id,
59+
phone_number=phone_number,
60+
),
61+
)
62+
except Exception as e:
63+
error_message = f"vapi_call function failed: {e}"
64+
raise NonRetryableError(error_message) from e
65+
else:
66+
return result
5667

5768
@agent.event
5869
async def end(self, end: EndEvent) -> EndEvent:

agent_telephony/vapi/agent_vapi/src/functions/llm_chat.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from openai import OpenAI
55
from pydantic import BaseModel, Field
6-
from restack_ai.function import function, log, stream_to_websocket
6+
from restack_ai.function import NonRetryableError, function, stream_to_websocket
77

88
from src.client import api_address
99

@@ -48,5 +48,5 @@ async def llm_chat(function_input: LlmChatInput) -> str:
4848
return await stream_to_websocket(api_address=api_address, data=response)
4949

5050
except Exception as e:
51-
log.error("llm_chat function failed", error=str(e))
52-
raise
51+
error_message = f"llm_chat function failed: {e}"
52+
raise NonRetryableError(error_message) from e

agent_todo/src/functions/get_random.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import secrets
22

33
from pydantic import BaseModel
4-
from restack_ai.function import function, log
4+
from restack_ai.function import NonRetryableError, function
55

66

77
class RandomParams(BaseModel):
@@ -13,7 +13,7 @@ async def get_random(params: RandomParams) -> str:
1313
try:
1414
random_number = secrets.randbelow(100)
1515
except Exception as e:
16-
log.error("random function failed", error=e)
17-
raise
16+
error_message = f"get_random function failed: {e}"
17+
raise NonRetryableError(error_message) from e
1818
else:
1919
return f"The random number for {params.todo_title} is {random_number}."

agent_todo/src/functions/get_result.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import secrets
22

33
from pydantic import BaseModel
4-
from restack_ai.function import function, log
4+
from restack_ai.function import NonRetryableError, function
55

66

77
class ResultParams(BaseModel):
@@ -20,5 +20,5 @@ async def get_result(params: ResultParams) -> ResultResponse:
2020
status = secrets.choice(["completed", "failed"])
2121
return ResultResponse(todo_id=params.todo_id, status=status)
2222
except Exception as e:
23-
log.error("result function failed", error=e)
24-
raise
23+
error_message = f"get_result function failed: {e}"
24+
raise NonRetryableError(error_message) from e

agent_todo/src/functions/todo_create.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import secrets
22

33
from pydantic import BaseModel
4-
from restack_ai.function import function, log
4+
from restack_ai.function import NonRetryableError, function, log
55

66

77
class TodoCreateParams(BaseModel):
@@ -15,8 +15,8 @@ async def todo_create(params: TodoCreateParams) -> str:
1515

1616
todo_id = f"todo-{secrets.randbelow(9000) + 1000}"
1717
except Exception as e:
18-
log.error("todo_create function failed", error=e)
19-
raise
18+
error_message = f"todo_create function failed: {e}"
19+
raise NonRetryableError(error_message) from e
2020
else:
2121
log.info("todo_create function completed", todo_id=todo_id)
2222
return f"Created the todo '{params.title}' with id: {todo_id}"

agent_tool/src/functions/lookup_sales.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Literal
22

33
from pydantic import BaseModel
4-
from restack_ai.function import function, log
4+
from restack_ai.function import NonRetryableError, function, log
55

66

77
class SalesItem(BaseModel):
@@ -105,5 +105,5 @@ async def lookup_sales(function_input: LookupSalesInput) -> LookupSalesOutput:
105105

106106
return LookupSalesOutput(sales=filtered_items)
107107
except Exception as e:
108-
log.error("lookup_sales function failed", error=e)
109-
raise
108+
error_message = f"lookup_sales function failed: {e}"
109+
raise NonRetryableError(error_message) from e

agent_video/src/agents/agent.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22

33
from pydantic import BaseModel
4-
from restack_ai.agent import agent, import_functions, log
4+
from restack_ai.agent import NonRetryableError, agent, import_functions, log
55

66
with import_functions():
77
from src.functions.context_docs import context_docs
@@ -26,13 +26,18 @@ async def messages(self, messages_event: MessagesEvent) -> list[Message]:
2626
log.info(f"Received message: {messages_event.messages}")
2727
self.messages.extend(messages_event.messages)
2828

29-
assistant_message = await agent.step(
30-
function=llm_chat,
31-
function_input=LlmChatInput(messages=self.messages),
32-
start_to_close_timeout=timedelta(seconds=120),
33-
)
34-
self.messages.append(Message(role="assistant", content=str(assistant_message)))
35-
return self.messages
29+
try:
30+
assistant_message = await agent.step(
31+
function=llm_chat,
32+
function_input=LlmChatInput(messages=self.messages),
33+
start_to_close_timeout=timedelta(seconds=120),
34+
)
35+
except Exception as e:
36+
error_message = f"llm_chat function failed: {e}"
37+
raise NonRetryableError(error_message) from e
38+
else:
39+
self.messages.append(Message(role="assistant", content=str(assistant_message)))
40+
return self.messages
3641

3742
@agent.event
3843
async def end(self, end: EndEvent) -> EndEvent:
@@ -42,11 +47,16 @@ async def end(self, end: EndEvent) -> EndEvent:
4247

4348
@agent.run
4449
async def run(self) -> None:
45-
docs = await agent.step(function=context_docs)
46-
system_prompt=f"""
47-
You are an interactive video assistant, your answers will be used in text to speech so try to keep answers short and concise so that interaction is seamless.
48-
You can answer questions about the following documentation:
49-
{docs}
50-
"""
51-
self.messages.append(Message(role="system", content=system_prompt))
52-
await agent.condition(lambda: self.end)
50+
try:
51+
docs = await agent.step(function=context_docs)
52+
except Exception as e:
53+
error_message = f"context_docs function failed: {e}"
54+
raise NonRetryableError(error_message) from e
55+
else:
56+
system_prompt=f"""
57+
You are an interactive video assistant, your answers will be used in text to speech so try to keep answers short and concise so that interaction is seamless.
58+
You can answer questions about the following documentation:
59+
{docs}
60+
"""
61+
self.messages.append(Message(role="system", content=system_prompt))
62+
await agent.condition(lambda: self.end)
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import aiohttp
2-
from restack_ai.function import function, log
2+
from restack_ai.function import NonRetryableError, function, log
33

44

55
async def fetch_content_from_url(url: str) -> str:
66
async with aiohttp.ClientSession() as session:
77
async with session.get(url) as response:
88
if response.status == 200:
99
return await response.text()
10-
log.error("Failed to fetch content", status=response.status)
11-
raise Exception(f"Failed to fetch content: {response.status}")
10+
error_message = f"Failed to fetch content: {response.status}"
11+
raise NonRetryableError(error_message)
1212

1313

1414
@function.defn()
@@ -20,5 +20,5 @@ async def context_docs() -> str:
2020
return docs_content
2121

2222
except Exception as e:
23-
log.error("llm_chat function failed", error=str(e))
24-
raise
23+
error_message = f"context_docs function failed: {e}"
24+
raise NonRetryableError(error_message) from e

agent_video/src/functions/llm_chat.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from openai import OpenAI
55
from pydantic import BaseModel, Field
6-
from restack_ai.function import function, log, stream_to_websocket
6+
from restack_ai.function import NonRetryableError, function, stream_to_websocket
77

88
from src.client import api_address
99

@@ -48,5 +48,5 @@ async def llm_chat(function_input: LlmChatInput) -> str:
4848
return await stream_to_websocket(api_address=api_address, data=response)
4949

5050
except Exception as e:
51-
log.error("llm_chat function failed", error=str(e))
52-
raise
51+
error_message = f"llm_chat function failed: {e}"
52+
raise NonRetryableError(error_message) from e

0 commit comments

Comments
 (0)