File size: 2,690 Bytes
ceb0714
 
 
 
275dbc9
ceb0714
 
 
 
 
 
 
275dbc9
df3fc47
 
 
 
 
 
 
 
 
 
 
 
 
 
2026377
df3fc47
ceb0714
 
 
 
 
 
 
 
 
 
 
 
 
b30dfe7
 
3b0acae
b30dfe7
 
3b0acae
 
 
ceb0714
 
275dbc9
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
{
 "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",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compress_bool_list(bools):\n",
    "    compressed_bits = np.packbits(bools,bitorder='big')\n",
    "    return [compressed_bits,len(bools)]\n",
    "\n",
    "def uncompress_bool_list(log):\n",
    "    log[0]=np.array(log[0],dtype=np.uint8)\n",
    "    return [bool(b) for b in np.unpackbits(log[0],bitorder='big')[:log[1]]]"
   ]
  },
  {
   "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"
   ]
  }
 ],
 "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
}