Skip to content

Commit

Permalink
docs(core): Add function execution doc
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyinc committed Nov 3, 2024
1 parent 50cb85d commit 2cc27fb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release-python-task.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Release Python Packages

on:
release:
types: [published]
push:
branches:
- main
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
name: Release Python

on:
release:
types: [published]
push:
branches:
- main
Expand Down
106 changes: 84 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,95 @@ pip install lyric-js-worker
import asyncio
from lyric import DefaultLyricDriver

lcd = DefaultLyricDriver(host="localhost", log_level="ERROR")
lcd.start()

# Load workers(default: Python, JavaScript)
asyncio.run(lcd.lyric.load_default_workers())

# Execute Python code
python_code = """
def add(a, b):
return a + b
result = add(1, 2)
print(result)
"""
async def main():
lcd = DefaultLyricDriver(host="localhost", log_level="ERROR")
lcd.start()

# Load workers(default: Python, JavaScript)
await lcd.lyric.load_default_workers()

# Execute Python code
python_code = """
def add(a, b):
return a + b
result = add(1, 2)
print(result)
"""

py_res = await lcd.exec(python_code, "python")
print(py_res)

# Execute JavaScript code
js_code = """
console.log('Hello from JavaScript!');
"""

js_res = await lcd.exec(js_code, "javascript")
print(js_res)

# Stop the driver
lcd.stop()

py_res = asyncio.run(lcd.exec(python_code, "python"))
print(py_res)
asyncio.run(main())
```

### Function Execution

# Execute JavaScript code
js_code = """
console.log('Hello from JavaScript!');
```python
import asyncio
import json
from lyric import DefaultLyricDriver

async def main():
lcd = DefaultLyricDriver(host="localhost", log_level="ERROR")
lcd.start()

# Load workers(default: Python, JavaScript)
await lcd.lyric.load_default_workers()
py_func = """
def message_handler(message_dict):
user_message = message_dict.get("user_message")
ai_message = message_dict.get("ai_message")
return {
"user": user_message,
"ai": ai_message,
"all": [user_message, ai_message],
"custom": "custom",
"handler_language": "python",
}
"""
input_data = {
"user_message": "Hello from user",
"ai_message": "Hello from AI",
}
input_bytes = json.dumps(input_data).encode("utf-8")
py_res = await lcd.exec1(py_func, input_bytes, "message_handler", lang="python")
# Get the result of the function execution
result_dict = py_res.output
print("Python result:", result_dict)
print(f"Full output: {py_res}")

js_func = """
function message_handler(message_dict) {
return {
user: message_dict.user_message,
ai: message_dict.ai_message,
all: [message_dict.user_message, message_dict.ai_message],
custom: "custom",
handler_language: "javascript",
};
}
"""
js_res = await lcd.exec1(js_func, input_bytes, "message_handler", lang="javascript")
# Get the result of the function execution
result_dict = js_res.output
print("JavaScript result:", result_dict)
print(f"Full output: {js_res}")

js_res = asyncio.run(lcd.exec(js_code, "javascript"))
print(js_res)
# Stop the driver
lcd.stop()

# Stop the driver
lcd.stop()
asyncio.run(main())
```

## 🔧 Requirements
Expand Down
6 changes: 3 additions & 3 deletions crates/lyric/src/lyric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Lyric {
env,
}) {
Err(e) => {
tracing::error!("core_call fatal error");
// tracing::error!("core_call fatal error");
return Err(Error::InternalError(
format!("Failed to send message to core: {}", e).to_string(),
));
Expand Down Expand Up @@ -347,7 +347,7 @@ impl Lyric {
) -> Result<T, Error> {
match self.inner.tx_to_core.send(msg) {
Err(e) => {
tracing::error!("core_call fatal error");
// tracing::error!("core_call fatal error");
return Err(Error::InternalError(
format!("Failed to send message to core: {}", e).to_string(),
));
Expand All @@ -357,7 +357,7 @@ impl Lyric {
match res_rx.await {
Ok(r) => r.map_err(|e| Error::APIError(e.into())),
Err(e) => {
tracing::error!("core_call fatal error");
// tracing::error!("core_call fatal error");
Err(Error::InternalError(
format!("Failed to receive response from core: {}", e).to_string(),
))
Expand Down

0 comments on commit 2cc27fb

Please sign in to comment.