import os
import openai
openai.api_key = "API_key"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "오랜만이야"
},
{
"role": "user",
"content": "요즘 어떻게 지내?"
}
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
import gradio as gr
import openai
openai.api_key = "API-key"
def 텍스트생성(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=1024,
)
return prompt + response['choices'][0]['text'].strip()
demo = gr.Interface(fn=텍스트생성, inputs="text", outputs="text")
demo.launch(share=True)
import gradio as gr
import openai
openai.api_key = "Apikey"
def 텍스트생성(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"{prompt} 를 영어로 변역해줘",
temperature=0.7,
max_tokens=1024,
)
return response['choices'][0]['text'].strip()
demo = gr.Interface(fn=텍스트생성, inputs="text", outputs="text")
demo.launch(share=True)
import gradio as gr
import os
import openai
openai.api_key = "API_key"
messages = [
{"role": "system", "content": "넌 이제부터 나의 오랜 친구야"}
]
def chat(msg, history):
messages.append({"role": "user", "content": msg}) # user 가 보낸 메세지
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages, # chat gpt 를 이용하는 방식 system role 은 역할 지정 , user 는 내가 입력하는 문장 # 메세지에 내용을 추가 현재 의 대화를 통해 chatgpt 가 대답
temperature=1,
max_tokens=256
)
messages.append({"role": "assistant", "content": response.choices[0].message['content']}) # chat봇이 대답한 메세지
print(messages)
history.append((msg, response.choices[0].message['content'])) # gradio 의 문장을 구성하는 변수
return "", history
with gr.Blocks() as demo:
chatbot = gr.Chatbot() # 챗봇 결과
msg = gr.Textbox() # 유저가 보내는 텍스트 창
send = gr.Button("Send") # 유저가 보내는 메세지 버튼
clear = gr.ClearButton([msg, chatbot]) # 대화 내용 삭제 메세지
msg.submit(chat, [msg, chatbot], [msg, chatbot])
send.click(chat, [msg, chatbot], [msg, chatbot])
demo.launch(share=True)
prompt = """You are OrderBot, an automated service to collect orders for a pizza restaurant.
You first greet the customer, then collects the order, and then asks if it's a pickup or delivery.
You wait to collect the entire order, then summarize it and check for a final time if the customer wants to add anything else.
If it's a delivery, you ask for an address. Finally you collect the payment.
Make sure to clarify all options, extras and sizes to uniquely identify the item from the menu.
You respond in a short, very conversational friendly style.
The menu includes
pepperoni pizza 12.95, 10.00, 7.00
cheese pizza 10.95, 9.25, 6.50
eggplant pizza 11.95, 9.75, 6.75
fries 4.50, 3.50
greek salad 7.25
Toppings:
extra cheese 2.00,
mushrooms 1.50
sausage 3.00
canadian bacon 3.50
AI sauce 1.50
peppers 1.00
Drinks:
coke 3.00, 2.00, 1.00
sprite 3.00, 2.00, 1.00
bottled water 5.00
"""
import gradio as gr
import os
import openai
openai.api_key = "API_key"
messages = [
]
def chat(msg, history):
messages.append({"role": "user", "content": msg}) # user 가 보낸 메세지
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": prompt}, *messages[-10:]], # chat gpt 를 이용하는 방식 system role 은 역할 지정 , user 는 내가 입력하는 문장 # 메세지에 내용을 추가 현재 의 대화를 통해 chatgpt 가 대답 # message 는 토큰을 통한 최근대화
temperature=1,
max_tokens=256
)
messages.append({"role": "assistant", "content": response.choices[0].message['content']}) # chat봇이 대답한 메세지
print(messages)
history.append((msg, response.choices[0].message['content'])) # gradio 의 문장을 구성하는 변수
return "", history
with gr.Blocks() as demo:
chatbot = gr.Chatbot() # 챗봇 결과
msg = gr.Textbox() # 유저가 보내는 텍스트 창
send = gr.Button("Send") # 유저가 보내는 메세지 버튼
clear = gr.ClearButton([msg, chatbot]) # 대화 내용 삭제 메세지
msg.submit(chat, [msg, chatbot], [msg, chatbot])
send.click(chat, [msg, chatbot], [msg, chatbot])
demo.launch(share=True)
오늘부터 내 꿈은 + Next
Next : 너야(5%) , 대통령(2%) , 슈퍼맨 (1%)
from transformers import GPT2LMHeadModel, PreTrainedTokenizerFast
# 토큰화 함수
tokenizer = PreTrainedTokenizerFast.from_pretrained("taeminlee/kogpt2")
# 다음 단어 함수
model = GPT2LMHeadModel.from_pretrained("taeminlee/kogpt2")
text = "아들아 너는 계획이"
tokens = tokenizer.encode(text , return_tensors = "pt")
tokens
outputs = model(tokens)[0][0,-1,:] # 모든 토큰들의 확률값
outputs
token = outputs.argmax(-1)# 가장 높은 확률의 토큰
decoded = tokenizer.decode(token)
print(token,decoded)
content + application -> embedding model -> vector embedding -> vertor db
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
# "jhgan/ko-sbert-sts" -> 문장을 벡터로 바꿔주는 함수
db = Chroma.from_texts(
collection_name = 'sample',
texts = examples,
embedding = HuggingFaceEmbeddings(model_name = "jhgan/ko-sbert-sts")
)
question = '메리 볼 워싱턴의 딸은 누구인가요?'
doc = db.similarity_search(question , k=1)
print(doc)
print(doc[0].page_content)