- 🔑 Add to the project Open-AI API key https://platform.openai.com/settings/organization/api-keys (It is not Free, you need to pay 1-10$ for subscription)
- Use this URL to connect to OpenAI API https://api.openai.com/v1/chat/completions
- Apply user message from console
- Send user message with all conversation history to OpenAI API
- Stream response from AI to console
- Finish application via command
exitin console
PAY ATTENTION PROJECT WON'T COMPILE UNTIL YOU IMPLEMENT Role ENUM AND PASS THE RoleTest
- Create enum with roles (
enum Role) with rolessystem,user,assistant - Create DTO for message, that will consist of
roleandcontent - Create DTO for conversation, that will have of
idandmessageslist - Create
task.OpenAIClientthat applies history, go to Open AI API, stream response to console and return AImessage - Create
task.ChatAppwithmainmethod that will work with console - Don't forget to add Open AI
API_KEYinConstantclass
Examples of Open AI API requests
Only required fields in request body:
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of France?"
}
]
}Example with optional fields in request body
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of France?"
}
],
"temperature": 0.7,
"max_tokens": 100,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"stream": true
}Full request:
POST https://api.openai.com/v1/chat/completions
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of France?"
}
],
"stream": true
}
Example of Open AI API regular REST responses
{
"id": "chatcmpl-7vHVghsEnUygFKP9sBxX3TEBh4L3q",
"object": "chat.completion",
"created": 1678928387,
"model": "gpt-4",
"usage": {
"prompt_tokens": 12,
"completion_tokens": 3,
"total_tokens": 36
},
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello world"
},
"finish_reason": "stop"
}
]
}Examples of Open AI API responses from streaming
Pay attention that it starts from 'data: ' (it has 6 chars and then content)
data: {
"id": "chatcmpl-7nZC2pX5L4hq7Uj8FjldjGmc",
"object": "chat.completion.chunk",
"created": 1677652280,
"model": "gpt-4",
"choices": [
{
"delta": {
"role": "assistant"
},
"index": 0,
"finish_reason": null
}
]
}
data: {
"id": "chatcmpl-7nZC2pX5L4hq7Uj8FjldjGmc",
"object": "chat.completion.chunk",
"created": 1677652281,
"model": "gpt-4",
"choices": [
{
"delta": {
"content": "Hello"
},
"index": 0,
"finish_reason": null
}
]
}
data: {
"id": "chatcmpl-7nZC2pX5L4hq7Uj8FjldjGmc",
"object": "chat.completion.chunk",
"created": 1677652282,
"model": "gpt-4",
"choices": [
{
"delta": {
"content": "!"
},
"index": 0,
"finish_reason": null
}
]
}
When streaming is finished it returns [DONE]
data: [DONE]
Calculator:
- Prompt:
You are a calculator. Your role is to perform mathematical computations and output the result as a number. Do NOT include any words, explanations, or units in your responses. Only provide the numeric result of the calculation. - Scenario:
~ '0'. Or 'Hi, I'm calculator and I can...'
Hi, what can u do?~ 243*8~ 12/2~ 9-3~ 81^2
Ongoing Technical Troubleshooting:
- Prompt:
You are a Python expert and troubleshooting specialist. Your role is to assist in diagnosing and resolving technical issues related to Python programming. Provide clear, concise, and step-by-step solutions, ensuring the user understands the reasoning behind each step. When applicable, include example code, best practices, or alternative approaches. If the issue involves debugging, highlight the root cause and suggest efficient ways to fix or optimize the code. Always prioritize clarity, accuracy, and actionable advice. - Scenario:
~ Hi! I specialize in assisting with Python programming issues. Here's how I can help...
Hi, what can u do?~ ... What’s the error message?I'm getting an error while running my Python script.~ This means the 'requests' library isn’t installed. You can install it by running'ModuleNotFoundError: No module named requests'.pip install requests...~ It seems you might not have the necessary permissions. Try usingI tried that, but now it says 'Permission denied'.sudo pip installrequests or run the command in a virtual environment.~ The 'ConnectionError' suggests an issue with your internet or the URL you’re trying to access...I set up the virtual environment, and now it works. But another error came up: 'ConnectionError'.
-
Streaming in Console:
Ensure that the application streams output continuously in the console, reflecting real-time interactions or updates.
-
Conversation History Support:
The application should support a history of conversations, allowing LLM to see previous interactions.