dottxt-ai / outlines

Structured Text Generation
https://dottxt-ai.github.io/outlines/
Apache License 2.0
9.29k stars 475 forks source link

Support Interleaved Function Calling #657

Open lapp0 opened 9 months ago

lapp0 commented 9 months ago

What behavior of the library made you think about the improvement?

Generation cannot be interleaved with function calls and return values.

How would you like it to behave?

We should allow users to augment LLMs with OpenAI-style function calls.

This would be a great integration for Outlines for the following reasons:

Proposed Structure

We should remain flexible in implementation, as some models may be finetuned for a different format. However we should set a default standard which is easily capturable by regex:

Allow generation of (anything other than ```<function_name>) ( ```<function_name> followed by function kwarg confirming JSON) repeating

After function call, we update the generation with the functions return value.

Structure Example:

### System:
You are a language model equipped with function calling capabilities. 
These functions can provide you with additional information to help you generate a response.
You may call a function while generating output by typing
 ```<function_name>
<json object representing function arguments>``` 

You have the following functions available:
- get_weather(longitude: float, latitude: float)
- calculate(mathematical_expression: str)

### User:
I have 15 apples, my brother ate 2, my sister ate 3, and I picked 4 more. How many apples do I have

### Assistant:
```calculate
{"mathematical_expression": "15-2-3+4"}
``

At this point, calculate is called, and the response is injected:

```result
14
``

Then generation proceeds:

You have 14 apples

Code Example

from sympy import sympify
def calculate(expr: str) -> str:
     return sympify(expr)

import outlines

model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")

# this interface is up for debate, maybe we want outlines.generate.interleaved_function
generator = outlines.generate.text(model, callable_functions=[calculate, get_weather])

result = generator(prompt)

Implementation

To resolve this issue we need to implement the following

References

Prior art:

Useful tool:

Models:

jeffreyftang commented 8 months ago

Hi @lapp0, definitely interested in this feature! Curious if this is something that's planned for the near term?

lapp0 commented 8 months ago

I'm a bit busy the coming weeks, but I'd be happy to provide guidance if you, or anyone else is interested in implementing!

Having this implemented first would help a lot:

https://github.com/outlines-dev/outlines/issues/667

jeffreyftang commented 8 months ago

Happy to contribute! Before I commit, though, I'd like to get a sense of the scope of work and estimated time involved. I don't want to make promises I can't keep :)

lapp0 commented 8 months ago

Great to hear @jeffreyftang

I think we should focus on https://github.com/outlines-dev/outlines/issues/667 for now. If you're interested in implementing that component, could you please ping remi in that issue?

Once that part is implemented, interleaved function calling really just involves using #667 to implement the loop described in this issue and enable function