Pythonでコード型の生成AIをAPIで試す

from openai import OpenAI

client = OpenAI(api_key="sk-hoge****")

prompt = """
# Pythonで、与えられた文字列を逆順にして返す関数を作ってください
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant that writes clean Python code.."},
        {"role": "user", "content": prompt}
    ],
    temperature=0.2,
)

print(response.choices[0].message.content)

$ python3 codegen.py
もちろんです!以下は、与えられた文字列を逆順にして返すPythonの関数です。

“`python
def reverse_string(s):
return s[::-1]

# 使用例
input_string = “こんにちは”
reversed_string = reverse_string(input_string)
print(reversed_string) # 出力: はこんに
“`

この関数 `reverse_string` は、スライスを使って文字列を逆順にしています。`s[::-1]` は、文字列 `s` の全ての文字を逆順に取得します。

model, message, tempratureを指定してリクエストしていることがわかります。
なるほど

AI生成ツールの比較

1. Copilot Chat: Microsoft(+openAI) コーディング補助
2. Claude: Anthropic 長文補助
3. Gemini: Google 汎用AI・検索統合
4. Cursor: AI搭載のIDE、コーディングに特化
5. Devin: AIソフトウェアエンジニア, 自律型のコード開発

GeminiはChatGTPに比べてレスポンスの文章量が少ない印象。ChatGTPの方が丁寧で参考になりそう。
Devinは色々触って試してみたい。

Definition of Scripts

Definition of Scripts
A causally coherent set of events.

1. Each event sets off, or causes, the next event.
2. The causal connections between events make sense.
3. The parts are actions or scenes in the world.

Restaurant Script
Script: restaurant
track: formal dining
props: tables, menu, check, money, F = food, P = place
roles: S = customer, W = waiter, C = cook, M = cashier, O = owner
entry: S is hungry, S has money
result: S has less money, O has more money, S is not hungry, S is pleased
scenes:

Thematic Role System

David went to the meeting with Ashok by car.

Thematic Role
verb : go
agent : David
coagent : Ashok
destination : meeting
conveyance : car

Prepositional Constraints
Preposition, Themantic Roles
by | agent, conveyance, location
for | beneficiary, duration
from | source
to | destination
with | coagent, instrument

e.g.
That was written by Ashok.
David went to New York by train
David stood by the statue.

formal logic

Why do we need formal logic?
Soundness: Only valid conclusions can be proven.
Completeness: All valid conclusions can be proven.

Vertebrate -> Bird -> Eagle, Bluebird, Penguin
If an animal has feathers, then it is a bird.
If an animal lays eggs and it files, then it is a bird.

Frame: How do we make sense of a sentence?

Ashok ate a frog.
subject: Ashok
object: a frog
time:
utensils:
object-alive: false
object-is: in-subject
subject-mood: happy

David ate a pizza at home:
subject: David
object: a pizza
time:
utensils:
object-alive: false
object-is: in-subject
subject-mood: happy

Angela ate lasagna with her dad last night at Olive Garden.
subject : Angela
object : lasagna
location : Olive Garden
time : night
utensils :
object-alive : false
object-is : in-subject
subject-mood : happy

Levels of Cognitive Architectures

Low Level <-> High Level
Hardware/Implementation Level(e.g. a brain, transister), Algrithm/Symbol Level(e.g. means-ends analysis, semantic networks), Task/Knowledge Level(e.g. selecting a pitch, playing baseball)

The layers of Watson: the physical computer searching and decision-making answering the inputted clue

Assumptions of a Cognitive Architecture
– Goal-oriented
– Rich, complex envrionment
– Significant knowledge
– Symbols and abstractions
– Flexible and function of the environment
– Learning

Architecture + Content = Behavior
Function for cognitive architectures: f:P* -> A
Percepts -> Action

SOAR
Procedural, Semantic, Episodic
-> Working Memory

Semantic Networks

A:B
x -> x :unchanged
y -> y :expanded
z -> z :deleted

lexically: nodes
structurally: directional links
semantically: application specific labels

Guards and prisonaers

similarity weight
5point for unchanged, 4 for reflected, 3 for rotated, 2 for scaled, 1 for deleted, 0 for shape changed