File size: 2,714 Bytes
ceb0714 275dbc9 ceb0714 275dbc9 ceb0714 b30dfe7 3b0acae b30dfe7 3b0acae ceb0714 275dbc9 ceb0714 9ee674e ceb0714 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import anvil.server\n",
"import openai\n",
"import pathlib\n",
"import textwrap\n",
"import google.generativeai as genai #comment this for local deployment and uncomment dummy def below\n",
"# class genai:\n",
"# pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def call_gemini(text,key):\n",
" # response=f'calling gemini with key {key} and text {text}'\n",
" # return response\n",
" genai.configure(api_key=key)\n",
" model = genai.GenerativeModel('gemini-pro')\n",
" try:\n",
" response = model.generate_content(text)\n",
" retval=response.text\n",
" except Exception as e:\n",
" return -1,str(e)\n",
" return 0,retval"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def call_gpt(prompt,key,model):\n",
" openai.api_key=key\n",
" try:\n",
" messages=[{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}]\n",
" messages+=[{\"role\": \"user\", \"content\": prompt}]\n",
" completions=openai.chat.completions.create( #for new version >.28 ) \n",
" # completions=openai.ChatCompletion.create(\n",
" model=model, \n",
" messages=messages)\n",
" # prediction=completions['choices'][0]['message']['content']\n",
" prediction=completions.choices[0].message.content.strip() # for new version >.28\n",
" except Exception as e:\n",
" return -1,str(e)\n",
" return 0,prediction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def encode_gemini(text,key):\n",
" genai.configure(api_key=key)\n",
" result = genai.embed_content(\n",
" model=\"models/embedding-001\",\n",
" content=text,\n",
" task_type=\"retrieval_document\",\n",
" title=\"Embedding of single string\")\n",
" return result['embedding']"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py310all",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|