General overview
I have a c# .net 8 Application where I am trying to implement a chat like behaviour using the OpenAI API with GPT4o Model
I have a collection (chatHistory) where I store every message be it of role ‘system’, ‘assistant’, ‘function’ or ‘user’
Also I have currently 2 functions:
CreateLiquidityPlan
- Generates a liquidity plan as an excel file for the specified yearsCurrentLiquidityStatus
- Provides information on the liquidity status on a specific date with regard to a specific event
CreateLiquidityPlan should be called on requests like “generate the liquidity plans for 2024 and 2025”
CurrentLiquidityStatus should be called on requests like “Where will we be at the end of the year when the order of Mr.X comes in?”
Execution
When I create a new conversation a new chatHistory is created and a System Prompt with general instructions like “Answer in the same language as the user asked.” is appended.
Then the user can do his prompt → “Where will we be at the end of the year when the order of Mr.X comes in?”
| role | content |
|---|---|
| system | Answer in the same language as the user asked. |
| user | Where will we be at the end of the year when the order of Mr.X comes in? |
The chat history is then sent to the ChatCompletion API which returns the request of a tool_call.
So i figure out which tool I should call, execute the code and I append a Function!!! message to the history containing the result.
| role | content |
|---|---|
| system | Answer in the same language as the user asked. |
| user | Where will we be at the end of the year when the order of Mr.X comes in? |
| function | [CurrentLiquidityStatus] - If the following occurs: ‘Mr.X order’ Then the following happens: ‘We end up with an annual surplus of 100.295,53 euros on 2024-12-31’. TODO: Generate a liquidity plan as an excel file for the specified year 2024 |
The history is again sent to the api…
Expectation
I now expect that because of the “TODO” the CreateLiquidityPlan should be requested so i can append the result to the chat history and after calling the ChatCompletion API again it will stop requesting tool_calls and finally create a assistant message.
| role | content |
|---|---|
| system | Answer in the same language as the user asked. |
| user | Where will we be at the end of the year when the order of Mr.X comes in? |
| function | [CurrentLiquidityStatus] - If the following occurs: ‘Mr.X order’ Then the following happens: ‘We end up with an annual surplus of 100.295,53 euros on 2024-12-31’. TODO: Generate a liquidity plan as an excel file for the specified year 2024 |
| function | [CreateLiquidityPlan] - A liquidity plan for the year 2024 was successfully created! |
| assistant | When the order of Mr.X comes in, we end up with an annual surplus of 100.295,53 euros on 2024-12-31. The liquidity plan for the year 2024 was created! |
Reality
OpenAI requests to call CurrentLiquidityStatus over and over again… So I built in a
loop detection but I think I’ve made a mistake somewhere in my thinking about how the whole function calling thing is supposed to work.
I would be grateful if someone here could help me as I have been struggling with this problem for some time now