Skip to main content

Contextual chat with GPT-3 of OpenAI API.

Project description

gpt3-contextual

Contextual chat with GPT-3 of OpenAI API.

🚀 Quick start

Install.

$ pip install gpt3-contextual

Make script as console.py.

import asyncio
from gpt3contextual import ContextualChat

async def main():
    cc = ContextualChat(
        "YOUR_OPENAI_APIKEY",
        username="Human",
        agentname="AI"
    )

    while True:
        text = input("human> ")
        resp, _ = await cc.chat("user1234567890", text)
        print(f"AI> {resp}")

asyncio.run(main())

Run console.py and chat with GPT-3.

human> hello
AI>  Hi, how can I help you?
human> I'm hungry
AI>  What would you like to eat?
human> sandwitches
AI>  What kind of sandwich would you like?
human> ham&egg        
AI>  Would you like that on a white or wheat bread?
human> wheet
AI>  Would you like anything else with your ham and egg sandwich on wheat bread?
human> Everything is fine, thank you.
AI>  Great! Your order has been placed. Enjoy your meal!

🧸 Usage

You can set parameters to customize conversation senario when you make the instance of ContextualChat. See also https://platform.openai.com/docs/api-reference/completions to understands some params.

  • api_key: str : API key for OpenAI API.
  • context_count: int : Count of turns(request and response) to use as the context. Default=6
  • username: str : Name of roll of user. Default=customer.
  • agentname: str = Name or role of agent(bot). Default=agent.
  • chat_description: str : Some conditions to be considered in the senario of conversation.
  • engine: str : The engine(model) to use. Default=text-davinci-003.
  • temperature: float : What sampling temperature to use, between 0 and 2. Default=0.5.
  • max_tokens: int : The maximum number of tokens to generate in the completion. Default=2000.
  • context_manager: ContextManager : Custom ContextManager.

🥪 How it works

As you know, Completion endpoint doesn't provide the features for keep and use context. So, simply, this library send the previous conversation histories with the request text like below.

Prompt on the 1st turn:

human:hello
AI:

Chatbot returns Hi, how can I help you?.

Prompt on the 2nd turn:

human:hello
AI:Hi, how can I help you?
human:I'm hungry
AI:

Chatbot returns What would you like to eat?

:

Prompt on the 6th turn:

human:sandwitches
AI:What kind of sandwich would you like?
human:ham&egg        
AI:Would you like that on a white or wheat bread?
human:wheet
AI:Would you like anything else with your ham and egg sandwich on wheat bread?
human:Everything is fine, thank you.
AI:

Chatbot returns Great! Your order has been placed. Enjoy your meal!

If you change the username and agentname the senario of conversation changes. And, setting chat_description also make effects on the situations.

Here is an example to simulate the conversation by brother and sister in Japanese.

cc = ContextualChat(
    openai_apikey,
    username="兄",
    agentname="妹",
    chat_description="これは兄と親しい妹との会話です。仲良しなので丁寧語を使わずに話してください。"
)

Prompt to be sent to OpenAI API is like bellow. chat_description is always set to the first line, no matter how many turns of conversation proceed.

これは兄と親しい妹との会話です。仲良しなので丁寧語を使わずに話してください。
兄:おはよー
妹:おはよー!今日の予定は?
兄:いつも通り、特にないよ
妹:じゃあ、今日は一緒に遊ぼうよ!
兄:何かしたいことある?
妹:うん、今日は映画を見よう!
兄:いいね。 どんなのがいい?
妹:思いついた!サスペンス映画がいいかな!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gpt3_contextual-0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file gpt3_contextual-0.1-py3-none-any.whl.

File metadata

  • Download URL: gpt3_contextual-0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for gpt3_contextual-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4456747f1cecf7c4abbb537ce5c46e49912b097c95bd8eea145b2b0ceab3499f
MD5 a1ec264f22c5af55b6ad5762869229d8
BLAKE2b-256 cb5ae35fbda39fd059b3edf6a2bfca9fd986a79b0bcaf44e2e3052ef095f5051

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page