Skip to content

Commit

Permalink
improve aiForth.py now support aiModel to select LLM
Browse files Browse the repository at this point in the history
  • Loading branch information
hcchengithub committed Jul 28, 2024
1 parent 5a6a1c0 commit 09059f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
43 changes: 24 additions & 19 deletions peforth/aiFORTH.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
From you .py or .ipynb run:
%run -i "C:\\...\\peforth\\notebook\\aiFORTH.py"
module_directory = os.path.dirname(peforth.__file__)
aiforth = os.path.join(module_directory, "aiFORTH.py")
aiModel = "gpt-3.5-turbo" # "gpt-3.5-turbo", "GeminiPro"
%run -i $aiforth
and then you can start talking to AI.
Expand All @@ -13,32 +16,35 @@
Note, when you %run, the kernel is different, and thus
magics are unknown. Be aware of this is why %f, %ai does
not work in there.
注意: 行尾緊貼著 ? 在 JupyterlAB 是取得 help 的 magic 所以或前
或後要多個空格以免打架。
"""
# %%time

import os, peforth
from IPython.display import display, Markdown
# from IPython import get_ipython 這樣不行,用 %run -i %pathname 即可。
from columbus_api import Columbus
columbus = Columbus()

# %%time
# 從外面定義好 aiModel 才 %run 進來,否則用這裡的 default 值。
if 'aiModel' not in locals():
aiModel = "gpt-3.5-turbo" # "gpt-3.5-turbo", "GeminiPro"

peforth.dictate("""
display constant display // ( -- obj ) Jupyternotebook display function
Markdown constant Markdown // ( -- obj ) Jupyternotebook Markdown class
get_ipython to @get_ipython \ 把 get_ipython 介紹給 peforth
""")

GEMINI = False
COLUMBUS = True

# Gemini Pro ------
if GEMINI:
gemini_llm = columbus.get_llm_gemini(
if aiModel == "GeminiPro":
peforth_llm = columbus.get_llm_gemini(
modelname="gemini-pro:generateContent",
api_key=os.getenv("GoogleAIStudio_API_KEY")
)
peforth.dictate("""
gemini_llm constant llm_function \ 把 llm 介紹給 peforth
peforth_llm constant llm_function \ 把 llm 介紹給 peforth
: llm_wrapper ( prompt -- complete ) // llm_wrapper for Gemini
trim llm_function :> (pop()) dup
str :> rfind("candidates")==-1
Expand All @@ -48,16 +54,15 @@
' llm_wrapper to @llm \ 把 llm_wrapper 介紹給 peforth
""");

# Columbus gpt-35-turbo --------------
if COLUMBUS:
columbus35_llm = columbus.get_llm_for_LangChain()
if aiModel == "gpt-3.5-turbo":
peforth_llm = columbus.get_llm_for_LangChain(
modelname="gpt-3.5-turbo",
openai_api_key=os.getenv("OPENAI_API_KEY")
)
peforth.dictate("""
columbus35_llm constant llm_function \ 把 llm 介紹給 peforth
: llm_wrapper ( prompt -- complete ) // llm_wrapper for Gemini
trim llm_function :> invoke(pop()) dup
str :> rfind("content")==-1
if str else :> content then ;
' llm_wrapper to @llm \ 把 llm_wrapper 介紹給 peforth
peforth_llm constant llm_object
: llm_wrapper trim llm_object :> invoke(pop()).content ;
' llm_wrapper to @llm
""");

peforth.dictate("""
Expand Down
4 changes: 3 additions & 1 deletion peforth/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__version__ = "1.31"

# 22:24 2024/07/28 "1.32" 改良 peforth\peforth\aiFORTH.py 可用 aiModel 選用 LLM model.
__version__ = "1.32"

0 comments on commit 09059f1

Please sign in to comment.