Code Monkey home page Code Monkey logo

chainlit-openai-functions's Introduction

Streaming chatbot with OpenAI functions

This chatbot utilizes OpenAI's function calling feature to invoke appropriate functions based on user input and stream the response back.

On top of the standard chat interface, the UI exposes the particular function called along with its arguments, as well as the response from the function.

The current configuration defines two OpenAI functions that can be called:

  • get_current_weather: returns the current weather for a given location. Example input: What's the weather like in New York?

    • Note that the API returns temperature in Celsius by default. The time zone is set for Europe/Berlin, but this can be changed in openai_functions.py
  • get_search_results: A langchain agent that uses SERP API as a tool to search the web. Example input: Search the web for the best restaurants in Berlin

Other than that, you can easily define your own functions and add them to the app (see Making changes).

Sample conversation that makes use of two different OpenAI functions in a row, utilizing the response from the first function as an argument for the second function:

alt text

If we expand the response, we can see the function calls and their arguments (the longitude and latitude were inferred from the location name returned by the search function):

alt text

Setup

Install the dependencies

pip install -r requirements.txt

Set up your OpenAI API key as an environment variable

Rename .env.example to .env and replace the placeholders with your API keys.

OPENAI_API_KEY=your-key
SERPAPI_API_KEY=your-key

Note: While the OpenAI API key is required, the SERP API key is not. If you don't have one, you can still use the get_current_weather function, but it will not be able to search the web and will return an error message instead.

You don't need an API key to use the function that returns the current weather.

Run the app

chainlit run app.py -w

Making changes

The app is configured to be easily scalable. If you'd like to add more OpenAI functions, you can do so in the following way:

  • Define the function schema in openai_function_schemas.py
  • Add the function definition to openai_functions.py
  • Add the function to FUNCTIONS_MAPPING in openai_functions.py
  • To test that it works, your function definition can return a hard-coded JSON.

chainlit-openai-functions's People

Contributors

dissorial avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

chainlit-openai-functions's Issues

context_length_exceeded

during some search requests, get this:

==================================================================================
2023-07-13 14:31:27 - message='OpenAI API response' path=https://api.openai.com/v1/chat/completions processing_ms=294 request_id=e7c59ac661bf046ea04241c2a16c0258 response_code=400
2023-07-13 14:31:27 - error_code=context_length_exceeded error_message="This model's maximum context length is 4097 tokens. However, your messages resulted in 4238 tokens (4143 in the messages, 95 in the functions). Please reduce the length of the messages or functions." error_param=messages error_type=invalid_request_error message='OpenAI API error received' stream_error=False
Error getting model response: This model's maximum context length is 4097 tokens. However, your messages resulted in 4238 tokens (4143 in the messages, 95 in the functions). Please reduce the length of the messages or functions.
2023-07-13 14:31:27 - 'async for' requires an object with aiter method, got NoneType
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/chainlit/utils.py", line 36, in wrapper
return await user_function(**params_values)
File "/root/Chainlit-OpenAI-Functions/app.py", line 121, in run_conversation
async for stream_resp in model_response:
TypeError: 'async for' requires an object with aiter method, got NoneType

==================================================================================

could you please set the token limit safeguard parameter in the code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.