add function that convert text file to markdown output in terminal
This commit is contained in:
parent
761e706707
commit
0f610f87ae
60
note_chat.py
Normal file
60
note_chat.py
Normal file
@ -0,0 +1,60 @@
|
||||
from dotenv import load_dotenv
|
||||
import os, sys
|
||||
from openai import OpenAI
|
||||
import time
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# initialize then client
|
||||
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
||||
DEEPSEEK_API_URL = os.getenv("DEEPSEEK_API_URL")
|
||||
MODEL = "deepseek-chat"
|
||||
|
||||
client = OpenAI(
|
||||
api_key=DEEPSEEK_API_KEY,
|
||||
base_url=DEEPSEEK_API_URL,
|
||||
)
|
||||
|
||||
MAX_RETRIES = 3
|
||||
RETRY_DELAY = 2 # seconds
|
||||
count = 0
|
||||
|
||||
system_prompt = "You are a helpful assistant. You can convert text notes to markdown format. These notes are \
|
||||
in Question and Answer format. for example: Create two namespaces and name them ns1 and ns2 \n k create ns ns1 \n \
|
||||
k create ns ns2. where k create ns ns1 and k create ns ns2 are answers. Sometoimees questions can be more than one line, \
|
||||
and each of Q&A are separated by a empty line. Please make question as header and fomrat ansers in correct markdown format. \
|
||||
Most of the ansers are code snippets. Please only return the markdown content. "
|
||||
content = ""
|
||||
with open("k8s_day10.txt", "r") as f:
|
||||
for line in f:
|
||||
content += line
|
||||
|
||||
def chat_with_deepseek(max_retries: int =MAX_RETRIES, retry_delay: int =RETRY_DELAY):
|
||||
count = 0
|
||||
for _ in range(max_retries):
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model=MODEL,
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": f"please covert notes below to markdown format: \n {content} "},
|
||||
],
|
||||
stream=True,
|
||||
)
|
||||
for chunk in response:
|
||||
text = chunk.choices[0].delta.content
|
||||
if text:
|
||||
sys.stdout.write(text)
|
||||
sys.stdout.flush()
|
||||
|
||||
break
|
||||
except Exception as e:
|
||||
count += 1
|
||||
if count < max_retries:
|
||||
print(f"An error occurre: {e}. Retrying in {retry_delay} seconds...")
|
||||
time.sleep(retry_delay)
|
||||
else:
|
||||
print("Max retries reached. Exiting.")
|
||||
break
|
||||
|
||||
chat_with_deepseek()
|
Loading…
x
Reference in New Issue
Block a user