oughtinc / ice

Interactive Composition Explorer: a debugger for compositional language model programs
https://ice.ought.org
MIT License
533 stars 67 forks source link

Unclear how to use tracing within applications #323

Open ArtemPt239 opened 1 year ago

ArtemPt239 commented 1 year ago

I tried running an example recipe with python hello.py - and it works perfectly.

However, I can not find any instructions on how to use tracing functionality not as a standalone demonstration, but inside other applications.

A simplest example would be:

hello.py:


from ice.recipe import recipe

async def say_hello(): return "Hello world!"

recipe.main(say_hello)


> test.py:
```python
from hello import say_hello
import asyncio

if __name__ == '__main__':
    print(asyncio.run(say_hello()))

Which does not work when run with python test.py - no traces being recorded.

Could anyone please point me towards a way to achieve this?

ArtemPt239 commented 1 year ago

For anyone stumbling into this issue later:

After digging into the code of ICE I came up with this SOLUTION:

hello.py:


from ice.trace import trace

@trace async def say_hello(): return "Hello world!"


> test.py:
```python
from hello import say_hello
import asyncio
from ice.trace import trace, enable_trace

if __name__ == '__main__':
    enable_trace()
    @trace
    async def main_wrapper():
        # A traced function cannot be called until the event loop is running.
        return await say_hello()

    print(asyncio.run(main_wrapper()))

This code performs tracing when started with python test.py

ArtemPt239 commented 1 year ago

Despite the solution being found - it may be benificial to mention this method of running tracing in the README. Hence I am leaving the issue open.