2024-10-22|閱讀時間 ‧ 約 0 分鐘

AI說書 - 從0開始 - 221 | GPT 4 & RAG 實作

我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。


回顧目前手上有的素材:


現在可以來實作 RAG 函數了:

from openai import OpenAI
import ipywidgets as widgets
from IPython.display import display

client = OpenAI()
AImodel = "gpt-4o"

def openai_chat(input_text, document_excerpt, web_article_summary):
response = client.chat.completions.create(model = AImodel,
messages = [{"role": "system",
"content": f"The following is an excerpt from a document about climate change: {document_excerpt}"},
{"role": "system",
"content": f"The following is a summary of a web article on renewable energy: {web_article_summary}"},
{"role": "user",
"content": input_text}],
temperature = 0.1,
max_tokens = 150,
top_p = 0.9,
frequency_penalty = 0.5, # Reduces repetition of the same text. Higher values discourage repetition
presence_penalty = 0.5) # Reduces repetition of similar topics. Higher values encourage new topics
return response.choices[0].message.content



分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.